---
title: "Replication Script: Individualized text messages about public services fail to sway voters: Evidence from a field experiment on Ugandan elections (Journal of Experimental Political Science)"
author: "Mark Buntaine & Ryan Jablonski (contributed to analysis)"
date: "4/26/2021"
output: 
  html_document:
    theme: united
    toc: true
---

##Notes

1. coefplot::buildModelCI.default uses clustered s.e. from felm() to build CIs. Likewise, stargazer reports clustered SEs from felm().


```{r setup, include=FALSE}

## Setting Up:

# RMarkdown options:
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
knitr::opts_chunk$set(results = "asis")

# Required ackages:
library(dplyr)
library(tidyverse)
library(coefplot)
library(gridExtra)
library(grid)
library(ggplot2)
library(lattice)
library(cowplot)
library(ggpubr)
library(estimatr)
library(ri2)
library(ggridges)
library(ggforce)
library(lfe) #Version 2.6-2291
#note on warnings, see: https://cran.r-project.org/web/packages/lfe/vignettes/identification.pdf
library(stargazer) #Version 5.2.1
library(readxl)
library(kableExtra)

```

```{r functions, message=FALSE}

# function to conduct randomization inference over an lfe::felm() model and save the coefficient on the treatment indicator:
felm.ri <- function(formula, dta, treat.var, rand.ob, rand.ob.info.cols, join.var, sims, ...){
  require(lfe)
  print("1. data and rand.ob must have rows organized in identical order")
  print("2. treat.var should be first right-side entry in formula")
  print("3. join.var must have identical name between data and rand.ob")
  
  ate <- coef(felm(formula, data=dta))[1]
  N <- felm(formula, data=dta)$N
  ate.samp.dist <- rep(NA,sims)
  
  for (i in 1:sims){
    dta[,treat.var] <- rand.ob[rand.ob[,join.var] %in% dta[,join.var], rand.ob.info.cols+i]
    
    ate.samp.dist[i] <- coef(felm(formula, data=dta))[1]
  }
  
  p.two.way <- sum(abs(ate)<abs(ate.samp.dist))/sims
  p.one.way.greater <- sum(ate<ate.samp.dist)/sims
  p.one.way.lesser <- sum(ate>ate.samp.dist)/sims
  se <- sd(ate.samp.dist)
  
  coun <- list("ate" = ate, "ate.samp.dist" = ate.samp.dist, "se"=se, "p.two.way" = p.two.way, "p.one.way.greater" = p.one.way.greater, "p.one.way.lesser" = p.one.way.lesser, "N" = N)
  return(coun)
}

# function to conduct randomization inference over an lfe::felm() model and save the full set of estimated coefficients:
felm.ri3 <- function(formula, dta, treat.var, rand.ob, rand.ob.info.cols, join.var, sims, ...){
  require(lfe)
  print("1. data and rand.ob must have rows organized in identical order")
  print("2. treat.var should be first right-side entry in formula")
  print("3. join.var must have identical name between data and rand.ob")
  
  N <- felm(formula, data=dta)$N
  coef_names <- labels(felm(formula, data=dta)$coefficients)[[1]]
  coef <- coef(felm(formula, data=dta))
  coef.num <- length(coef)
  coef.samp.dist <- matrix(data=NA, nrow=coef.num, ncol=sims)
  
  for (i in 1:sims){
    dta[,treat.var] <- rand.ob[rand.ob[,join.var] %in% dta[,join.var], rand.ob.info.cols+i]
    coef.samp.dist[,i] <- coef(felm(formula, data=dta))
  }
  
  se <- apply(coef.samp.dist, 1, sd) #Cannot get SEs off non-randomized parameters
  
  p.two.way <- rep(NA,length(coef))
  p.one.way.greater <- rep(NA,length(coef))
  p.one.way.lesser <- rep(NA,length(coef))
  
  for (i in 1:length(coef)){
    p.two.way[i] <- sum(abs(coef[i])<abs(coef.samp.dist[i,]))/sims
    p.one.way.greater[i] <- sum(coef[i]<coef.samp.dist[i,])/sims
    p.one.way.lesser[i] <- sum(coef[i]>coef.samp.dist[i,])/sims
  }
  
  coun <- list("coef" = coef, "se"=se, "p.two.way" = p.two.way, "p.one.way.greater" = p.one.way.greater, "p.one.way.lesser" = p.one.way.lesser, "coef.names"=coef_names, "N" = N) # "coef.samp.dist" = coef.samp.dist
  #not returning coef.samp.dist to save memory on large runs
  return(coun)
}

# function to impute mean value by cluster, returning imputed vector:
mean.imp <- function(data,var,cluster){
  for (i in 1:length(unique(data[,cluster]))){
    imp.value <- mean(data[data[,cluster]==unique(data[,cluster])[i], var], na.rm=T)
    data[data[,cluster]==unique(data[,cluster])[i] & is.na(data[,var]), var] <- imp.value
  }
  return(data[,var])
} 

# function to impute mean value by cluster, overwriting previous value:
d_mean.imp <- function(data,var,cluster){
  for (i in 1:length(unique(data[,cluster]))){
    imp.value <- mean(data[data[,cluster]==unique(data[,cluster])[i], var], na.rm=T)
    data[data[,cluster]==unique(data[,cluster])[i] & is.na(data[,var]), var] <- imp.value
  }
  return(data)
} 

```

```{r data-read-initial}

data <- read.csv("./analysis_data.csv")

#note: variables `subcounty` and `subcounty.x` are based on county names reported by the electoral commission and census respectively. We anonymized these variables separately in compiling the analysis data. We have confirmed that only minor differences in spelling account for the differences in the anonymized hashes.

sms_survey <- read_excel("./sms_experience_survey.xlsx")

lc3_councillor_vote_share <- read.csv("./lc3_councillor_vote_shares.csv")
lc3_chair_vote_share <- read.csv("./lc3_chair_vote_shares.csv")

lc5_councillor_vote_share=read.csv("./lc5_councillor_vote_shares.csv")
lc5_chair_vote_share=read.csv("./lc5_chair_vote_shares.csv")

```

```{r ri-chunks, message=FALSE, eval=FALSE}

# Note: This code chunk is NOT RUN, but preserved for replication purposes. It runs randomization inference over every model specified in the dataframes 'analysis_chunks' and 'analysis_chunks_hte'. It returns all information defined in the functions `felm.ri` and `felm.ri3` for each model into a single list object that is written externally at the end of the block. This chunk will take 1-3 days day to compile on a modern desktop computer. The output of this block is saved in the replication files and read in as data in the next code chunk for convenience. Running this block also requires the storage in memory of a large dataframe of random assignments 'ps_ri'.

# dataframe of 10,000 random assignments for the treatment:
ps_ri <- read.csv("./PS_RI.csv")

# ordering the data and RI dataframe in the same way:
ps_ri <- ps_ri[order(ps_ri$id.cleaned),]
data <- data[order(data$id.cleaned),]

# dataframes that contain model specifications:
analysis_chunks <- read.csv("./analysis_chunks.csv")
analysis_chunks_hte <- read.csv("./analysis_chunks-hte.csv")

# function that will run randomization inference using function `felm.ri` over each model specified in the dataframe of models:
ri_chunk <- function(chunk_dta, treat.var = "ps.treat", rand.ob = ps_ri, 
                     rand.ob.info.cols = 4, join.var = "id.cleaned", sims = 9999, ...){
  ri.runs <- nrow(chunk_dta)
  my_list <- list()
  for (i in 1:ri.runs){
    my_list[[i]] <- felm.ri(formula = as.formula(chunk_dta$formula[i]),
                      dta = eval(parse(text=chunk_dta$data[i])),
                      treat.var = treat.var,
                      rand.ob = rand.ob,
                      rand.ob.info.cols = rand.ob.info.cols,
                      join.var = join.var,
                      sims = sims)
  }
  names(my_list) <- chunk_dta$label
  return(my_list)
}

# function that will run randomization inference using function `felm.ri3` over each model specified in the dataframe of models:
ri_chunk_multi <- function(chunk_dta, treat.var = "ps.treat", rand.ob = ps_ri, 
                     rand.ob.info.cols = 4, join.var = "id.cleaned", sims = 9999, ...){
  ri.runs <- nrow(chunk_dta)
  my_list <- list()
  for (i in 1:ri.runs){
    my_list[[i]] <- felm.ri3(formula = as.formula(chunk_dta$formula[i]),
                      dta = eval(parse(text=chunk_dta$data[i])),
                      treat.var = treat.var,
                      rand.ob = rand.ob,
                      rand.ob.info.cols = rand.ob.info.cols,
                      join.var = join.var,
                      sims = sims)
  }
  names(my_list) <- chunk_dta$label
  return(my_list)
}

# running and saving the chunked RI outputs:
ri_out_direct <- ri_chunk(chunk_dta = analysis_chunks)
#saveRDS(ri_out_direct, file = "ri_out_direct.rds")

ri_out_hte <- ri_chunk_multi(chunk_dta = analysis_chunks_hte)
#saveRDS(ri_out_hte, file = "ri_out_hte.rds")

```

```{r ri-out-read}

# note: this chunk imports the outputs of the previous chunk, useful to eliminate the step of long RI running time:

analysis_chunks <- read.csv("./analysis_chunks.csv")
analysis_chunks_hte <- read.csv("./analysis_chunks-hte.csv")

ri_out_direct <- readRDS("ri_out_direct.rds")
ri_out_hte <- readRDS("ri_out_hte.rds")
```

# Main manuscript

## Figure 1: Direct effects of treatment with good and bad news subgroups

```{r figure1, fig.height=12, fig.width = 10}

# function to extract ATE from list object `ri_out_direct`
ate_grab_ri <- function(x){
  x$ate[names(x$ate)=="ps.treat"]
} #change to RI

# function to extract SE from list object `ri_out_direct`
se_grab_ri <- function(x){
  x$se[names(x$ate)=="ps.treat"]
}

# creating a dataframe of model outputs for plotting:
results_out <- tibble(names(ri_out_direct)) %>% rename(label="names(ri_out_direct)") %>%
  mutate(hypothesis=substr(label,1,3)) %>%
  mutate(outcome_var=str_extract(analysis_chunks$formula, "[^ ~]+")) %>%
  mutate(outcome_var=factor(outcome_var,
                               levels=unique(outcome_var))) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(ri_out_direct, ate_grab_ri)) %>%
  mutate(se=sapply(ri_out_direct,se_grab_ri)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

# The next blocks of code use the dataframe `results_out` to make the individual panels in Figure 1:

results_out_h1a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h1a")

h1a <- ggplot(data = results_out_h1a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h1a$outcome_var),
                     labels = c("LC5 Chair","LC5 Councillor", 
                                "LC3 Chair", "LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-.085,0.07) +
  ggtitle("Vote for Incumbent, Good News")

results_out_h1b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h1b")

h1b <- ggplot(data = results_out_h1b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h1b$outcome_var),
                     labels = c("LC5 Chair","LC5 Councillor", 
                                "LC3 Chair", "LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-.085,0.07) +
  ggtitle("Vote for Incumbent, Bad News")


results_out_h3a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h3a")

h3a <- ggplot(data = results_out_h3a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h3a$outcome_var),
                     labels = c("LC5 Chair","LC3 Chair")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.105,0.085) +
  ggtitle("Belief in Candidate Integrity, Good News")


results_out_h3b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h3b")

h3b <- ggplot(data = results_out_h3b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h3b$outcome_var),
                     labels = c("LC5 Chair","LC3 Chair")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.105,0.085) +
  ggtitle("Belief in Candidate Integrity, Bad News")


results_out_h4a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h4a")

h4a <- ggplot(data = results_out_h4a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h4a$outcome_var),
                     labels = c("LC5 Councillor","LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.105,0.075) +
  ggtitle("Belief in Candidate Effort, Good News")


results_out_h4b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h4b")

h4b <- ggplot(data = results_out_h4b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h4a$outcome_var),
                     labels = c("LC5 Councillor","LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.105,0.075) +
  ggtitle("Belief in Candidate Effort, Bad News")


results_out_h7a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h7a")

h7a <- ggplot(data = results_out_h7a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h7a$outcome_var),
                     labels = c("LC5","LC3")) +
    labs(x = "Election Type", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.04,0.06) +
  ggtitle("Turnout, Good News")


results_out_h7b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h7b")

h7b <- ggplot(data = results_out_h7b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h7b$outcome_var),
                     labels = c("LC5","LC3")) +
    labs(x = "Election Type", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.04,0.06) +
  ggtitle("Turnout, Bad News")

#pdf(file="combined-direct-cov-200610.pdf", height=12, width = 10)
ggarrange(h1a,h1b,h3a,h3b,h4a,h4b,h7a,h7b,
          ncol = 2, nrow = 4,
          labels = c("A","B","C","D","E","F","G","H"),
          common.legend = TRUE, legend = "bottom")
#dev.off()

```

## Figure 2. Conditional effects of treatment with good and bad news subgroups

The main text reports treatment effects within levels of hypothesized moderators pooled across offices. As described in the main text and SI Section 12, this approach is used for each of expositions. The first step is to estimate all the relevant HTEs by pooling across offices and looping through the levels of the moderators. The resulting objects are compiled and collected in the next code chunk.

```{r h2-pooled, message=FALSE, warning=FALSE}

#pooling outcome data across offices:
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
     mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                               office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                               office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                               office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

# creating a subsetting indicator in the pooled data to match subsets used to estimate direct treatment effects:
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0 & is.na(dta$lc5.councillor.redistricted2016), 1, keep)
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0 & is.na(dta$lc3.chair.redistricted), 1, keep)
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0 & is.na(dta$lc3.councillor.redistricted2016), 1, keep)

# executing the subsetting:
keep <- ifelse(dta$finalps.prior!=round(dta$finalps.prior), 0, keep) #removing imputed values of the moderator

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

# pooled regression, looping through base levels:
dta$finalps.prior.diff <- as.factor(dta$finalps.prior.diff)
l <- levels(dta$finalps.prior.diff) #-4 to 4

#without covariates:
for (i in 5:9){ #levels relevant to good news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h2a_",l[i]), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", "")),
              h2b.ps.pooled)
}

#with covariates:
for (i in 5:9){ #levels relevant to good news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h2a_",l[i],"_cov"), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", ""),"_cov"),
              h2b.ps.pooled)
}

#collecting both with/without cov output into list:
h2_out <- list(h2a_0,
               h2a_1,
               h2a_2,
               h2a_3,
               h2a_4,
               h2b_0,
               h2b_1,
               h2b_2,
               h2b_3,
               h2b_4,
               h2a_0_cov,
               h2a_1_cov,
               h2a_2_cov,
               h2a_3_cov,
               h2a_4_cov,
               h2b_0_cov,
               h2b_1_cov,
               h2b_2_cov,
               h2b_3_cov,
               h2b_4_cov
               )

h2_out_labels <- c("h2a_0",
                   "h2a_1",
                   "h2a_2",
                   "h2a_3",
                   "h2a_4",
                   "h2b_0",
                   "h2b_1",
                   "h2b_2",
                   "h2b_3",
                   "h2b_4",
                   "h2a_0_cov",
                   "h2a_1_cov",
                   "h2a_2_cov",
                   "h2a_3_cov",
                   "h2a_4_cov",
                   "h2b_0_cov",
                   "h2b_1_cov",
                   "h2b_2_cov",
                   "h2b_3_cov",
                   "h2b_4_cov"
               )

names(h2_out) <- h2_out_labels

```

```{r h5-pooled, message=FALSE, warning=FALSE}

#pooling outcome data across offices:
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(finalps.certainty = factor(finalps.certainty,
                                    levels=rev(c("very_uncertain","not_certain",
                                                 "certain","very_certain"))))

# subsetting the pooled data to match subsets used to estimate direct treatment effects:
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0, 1, keep)
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0, 1, keep)
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0, 1, keep)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$finalps.certainty) #very_certain to very_uncertain

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))

h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h5a_",i), h5a.ps.pooled)

h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h5b_",i), h5b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))

h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h5a_",i,"_cov"), h5a.ps.pooled)

h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h5b_",i,"_cov"), h5b.ps.pooled)
}

# collecting model outputs into a list:

h5_out <- list(h5a_1,
               h5a_2,
               h5a_3,
               h5a_4,
               h5b_1,
               h5b_2,
               h5b_3,
               h5b_4,
               h5a_1_cov,
               h5a_2_cov,
               h5a_3_cov,
               h5a_4_cov,
               h5b_1_cov,
               h5b_2_cov,
               h5b_3_cov,
               h5b_4_cov
            )

h5_out_labels <- c("h5a_1",
                   "h5a_2",
                   "h5a_3",
                   "h5a_4",
                   "h5b_1",
                   "h5b_2",
                   "h5b_3",
                   "h5b_4",
                   "h5a_1_cov",
                   "h5a_2_cov",
                   "h5a_3_cov",
                   "h5a_4_cov",
                   "h5b_1_cov",
                   "h5b_2_cov",
                   "h5b_3_cov",
                   "h5b_4_cov"
            )

names(h5_out) <- h5_out_labels

```

```{r h6-pooled, message=FALSE, warning=FALSE, fig.width=8}

# Moderator (ps.importance) item: When you decide how to vote for your LC5 chairperson and councilor, how important is the quality of public services like primary schools, water access, and health clinics? (1) not important, (2) not very important, (3) somewhat important, (4) very important, (8) don’t know, (9) refused to answer

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(ps.importance = factor(ps.importance, levels=c("very_important","somewhat_impor","not_very_impor","not_important")))


# subsetting the pooled data to match subsets used to estimate direct treatment effects:
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0 & is.na(dta$lc5.councillor.redistricted2016), 1, keep)
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0 & is.na(dta$lc3.chair.redistricted), 1, keep)
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0 & is.na(dta$lc3.councillor.redistricted2016), 1, keep)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- rev(levels(dta$ps.importance)) #not_important to very_important

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))

h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h6a_",i), h6a.ps.pooled)

h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h6b_",i), h6b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))

h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h6a_",i,"_cov"), h6a.ps.pooled)

h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h6b_",i,"_cov"), h6b.ps.pooled)
}

#collecting model outputs:
h6_out <- list(h6a_1,
               h6a_2,
               h6a_3,
               h6a_4,
               h6b_1,
               h6b_2,
               h6b_3,
               h6b_4,
               h6a_1_cov,
               h6a_2_cov,
               h6a_3_cov,
               h6a_4_cov,
               h6b_1_cov,
               h6b_2_cov,
               h6b_3_cov,
               h6b_4_cov
            )

h6_out_labels <- c("h6a_1",
                   "h6a_2",
                   "h6a_3",
                   "h6a_4",
                   "h6b_1",
                   "h6b_2",
                   "h6b_3",
                   "h6b_4",
                   "h6a_1_cov",
                   "h6a_2_cov",
                   "h6a_3_cov",
                   "h6a_4_cov",
                   "h6b_1_cov",
                   "h6b_2_cov",
                   "h6b_3_cov",
                   "h6b_4_cov"
            )

names(h6_out) <- h6_out_labels

```

```{r h8-pooled, message=FALSE, warning=FALSE, fig.width=8}

# major difference in this chunk and other pooled analysis chunks is the creation of the pooled 'aligned.inc' variable and pooling at the level of election

#pooling data
dta <- data %>% gather(e_level,turnout,c("lc5.turnout","lc3.turnout")) %>%
                mutate(turnout.intent = 
                         case_when(e_level=="lc5.turnout" ~ lc5.turnout.intent,
                                   e_level=="lc3.turnout" ~ lc3.turnout.intent)) %>%
  mutate(aligned.inc = case_when(
    e_level=="lc5.turnout" ~ aligned.lc5.chair.inc,
    e_level=="lc3.turnout" ~ aligned.lc3.chair.inc)) %>%
  mutate(aligned.inc = factor(aligned.inc, labels = c("not_aligned", "aligned"))) 

#pooled regression, looping through base levels
l <- levels(dta$aligned.inc) #not_aligned & aligned

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))

h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
assign(paste0("h8a_",i), h8a.ps.pooled)

h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
assign(paste0("h8b_",i), h8b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))

h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
assign(paste0("h8a_",i,"_cov"), h8a.ps.pooled)

h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
assign(paste0("h8b_",i,"_cov"), h8b.ps.pooled)
}

#collecting model outputs:
h8_out <- list(h8a_1,
               h8a_2,
               h8b_1,
               h8b_2,
               h8a_1_cov,
               h8a_2_cov,
               h8b_1_cov,
               h8b_2_cov
            )

h8_out_labels <- c("h8a_1",
                   "h8a_2",
                   "h8b_1",
                   "h8b_2",
                   "h8a_1_cov",
                   "h8a_2_cov",
                   "h8b_1_cov",
                   "h8b_2_cov"
            )

names(h8_out) <- h8_out_labels

```

```{r h9-pooled, message=FALSE, warning=FALSE}

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

#subsetting on pooled data, only using LC5 councillors
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0 & is.na(dta$lc5.councillor.redistricted2016), 1, 0)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(lc5.councillor.same.tribe = factor(lc5.councillor.same.tribe,  
                labels = c("not same tribe", "same tribe")))

l <- rev(levels(dta$lc5.councillor.same.tribe)) #same tribe & not same tribe

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(lc5.councillor.same.tribe = relevel(lc5.councillor.same.tribe, ref=l[i]))

h9a.ps.pooled <- felm(inc.vote ~ ps.treat*lc5.councillor.same.tribe + intent | location.id | 0 | id.cleaned, #note: no pooled outcomes
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h9a_",i), h9a.ps.pooled)

h9b.ps.pooled <- felm(inc.vote ~ ps.treat*lc5.councillor.same.tribe + intent | location.id | 0 | id.cleaned, #note: no pooled outcomes
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h9b_",i), h9b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(lc5.councillor.same.tribe = relevel(lc5.councillor.same.tribe, ref=l[i]))

h9a.ps.pooled <- felm(inc.vote ~ ps.treat*lc5.councillor.same.tribe + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, #note: no pooled outcomes
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h9a_",i,"_cov"), h9a.ps.pooled)

h9b.ps.pooled <- felm(inc.vote ~ ps.treat*lc5.councillor.same.tribe + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, #note: no pooled outcomes
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h9b_",i,"_cov"), h9b.ps.pooled)
}

#collecting model outputs:
h9_out <- list(h9a_1,
               h9a_2,
               h9b_1,
               h9b_2,
               h9a_1_cov,
               h9a_2_cov,
               h9b_1_cov,
               h9b_2_cov
            )

h9_out_labels <- c("h9a_1",
                   "h9a_2",
                   "h9b_1",
                   "h9b_2",
                   "h9a_1_cov",
                   "h9a_2_cov",
                   "h9b_1_cov",
                   "h9b_2_cov"
            )

names(h9_out) <- h9_out_labels

```

```{r h10-pooled, message=FALSE, warning=FALSE, fig.width=8}

# Moderator (lc5.received.gifts | lc3.received.gifts) item: 

# Have any representatives of LC5 officials provided gifts or money to you or your family in the few months before the election? (1) yes, (2) no, (3) don’t know, (4) refused.

# Have any representatives of LC3 officials provided gifts or money to you or your family in the few months before the election? (1) yes, (2) no, (3) don’t know, (4) refused

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(received.gifts = 
           case_when(office=="lc5.chair.inc.vote" ~ lc5.received.gifts,
                     office=="lc5.councillor.inc.vote" ~ lc5.received.gifts,
                     office=="lc3.chair.inc.vote" ~ lc3.received.gifts,
                     office=="lc3.councillor.inc.vote" ~ lc3.received.gifts))

#subsetting on pooled data
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0 & is.na(dta$lc5.councillor.redistricted2016), 1, keep)
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0 & is.na(dta$lc3.chair.redistricted), 1, keep)
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0 & is.na(dta$lc3.councillor.redistricted2016), 1, keep)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(received.gifts = factor(received.gifts,  
                labels = c("no gifts", "received gifts")))

l <- rev(levels(dta$received.gifts)) #received gifts & no gifts

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))

h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h10a_",i), h10a.ps.pooled)

h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h10b_",i), h10b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))

h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h10a_",i,"_cov"), h10a.ps.pooled)

h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h10b_",i,"_cov"), h10b.ps.pooled)
}

#collecting model outputs:
h10_out <- list(h10a_1,
                h10a_2,
                h10b_1,
                h10b_2,
                h10a_1_cov,
                h10a_2_cov,
                h10b_1_cov,
                h10b_2_cov
            )

h10_out_labels <- c("h10a_1",
                    "h10a_2",
                    "h10b_1",
                    "h10b_2",
                    "h10a_1_cov",
                    "h10a_2_cov",
                    "h10b_1_cov",
                    "h10b_2_cov"
            )

names(h10_out) <- h10_out_labels

```

```{r figure2, fig.height=12, fig.width=10}

hte_out <- c(h2_out,h5_out,h6_out,h8_out,h9_out,h10_out) 

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

#h2=inc.vote
#h5=inc.vote
#h6=inc.vote
#h8=turnout
#h9=inc.vote
#h10=inc.vote

results_out <- tibble(names(hte_out)) %>% rename(label="names(hte_out)") %>%
  mutate(hypothesis=word(label,1,sep = "\\_")) %>%
  mutate(outcome_var=ifelse(hypothesis %in% c("h8a","h8b"),"turnout","inc.vote")) %>%
  mutate(mod_label = case_when(label=="h2a_4" ~ "+4", 
                               label=="h2a_3" ~ "+3",
                               label=="h2a_2" ~ "+2", 
                               label=="h2a_1" ~ "+1",
                               label=="h2a_0" ~ "same",
                               label=="h2b_0" ~ "same", 
                               label=="h2b_1" ~ "-1",
                               label=="h2b_2" ~ "-2", 
                               label=="h2b_3" ~ "-3",
                               label=="h2b_4" ~ "-4",
                               label=="h5a_4" ~ "Very\nUncertain", 
                               label=="h5a_3" ~ "Not\nCertain",
                               label=="h5a_2" ~ "Certain", 
                               label=="h5a_1" ~ "Very\nCertain",
                               label=="h5b_4" ~ "Very\nUncertain", 
                               label=="h5b_3" ~ "Not\nCertain",
                               label=="h5b_2" ~ "Certain", 
                               label=="h5b_1" ~ "Very\nCertain",
                               label=="h6a_1" ~ "Not\nImportant", 
                               label=="h6a_2" ~ "Not Very\nImportant",
                               label=="h6a_3" ~ "Somewhat\nImportant", 
                               label=="h6a_4" ~ "Very\nImportant",
                               label=="h6b_1" ~ "Not\nImportant", 
                               label=="h6b_2" ~ "Not Very\nImportant",
                               label=="h6b_3" ~ "Somewhat\nImportant", 
                               label=="h6b_4" ~ "Very\nImportant",
                               label=="h8a_2" ~ "Aligned", 
                               label=="h8a_1" ~ "Not Aligned",
                               label=="h8b_2" ~ "Aligned", 
                               label=="h8b_1" ~ "Not Aligned",
                               label=="h9a_1" ~ "same tribe", 
                               label=="h9a_2" ~ "not same tribe",
                               label=="h9b_1" ~ "same tribe", 
                               label=="h9b_2" ~ "not same tribe",
                               label=="h10a_1" ~ "Received Gifts", 
                               label=="h10a_2" ~ "No Gifts",
                               label=="h10b_1" ~ "Received Gifts", 
                               label=="h10b_2" ~ "No Gifts",
                               label=="h2a_4_cov" ~ "+4", 
                               label=="h2a_3_cov" ~ "+3",
                               label=="h2a_2_cov" ~ "+2", 
                               label=="h2a_1_cov" ~ "+1",
                               label=="h2a_0_cov" ~ "same",
                               label=="h2b_0_cov" ~ "same", 
                               label=="h2b_1_cov" ~ "-1",
                               label=="h2b_2_cov" ~ "-2", 
                               label=="h2b_3_cov" ~ "-3",
                               label=="h2b_4_cov" ~ "-4",
                               label=="h5a_4_cov" ~ "Very\nUncertain", 
                               label=="h5a_3_cov" ~ "Not\nCertain",
                               label=="h5a_2_cov" ~ "Certain", 
                               label=="h5a_1_cov" ~ "Very\nCertain",
                               label=="h5b_4_cov" ~ "Very\nUncertain", 
                               label=="h5b_3_cov" ~ "Not\nCertain",
                               label=="h5b_2_cov" ~ "Certain", 
                               label=="h5b_1_cov" ~ "Very\nCertain",
                               label=="h6a_1_cov" ~ "Not\nImportant", 
                               label=="h6a_2_cov" ~ "Not Very\nImportant",
                               label=="h6a_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6a_4_cov" ~ "Very\nImportant",
                               label=="h6b_1_cov" ~ "Not\nImportant", 
                               label=="h6b_2_cov" ~ "Not Very\nImportant",
                               label=="h6b_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6b_4_cov" ~ "Very\nImportant",
                               label=="h8a_2_cov" ~ "Aligned", 
                               label=="h8a_1_cov" ~ "Not Aligned",
                               label=="h8b_2_cov" ~ "Aligned", 
                               label=="h8b_1_cov" ~ "Not Aligned",
                               label=="h9a_1_cov" ~ "same tribe", 
                               label=="h9a_2_cov" ~ "not same tribe",
                               label=="h9b_1_cov" ~ "same tribe", 
                               label=="h9b_2_cov" ~ "not same tribe",
                               label=="h10a_1_cov" ~ "Received Gifts", 
                               label=="h10a_2_cov" ~ "No Gifts",
                               label=="h10b_1_cov" ~ "Received Gifts", 
                               label=="h10b_2_cov" ~ "No Gifts"
                               )) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(hte_out, coef_grab)) %>%
  mutate(se=sapply(hte_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h2a <- results_out %>%
  filter(hypothesis=="h2a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","+1","+2","+3","+4")))

h2a <- ggplot(data = results_out_h2a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h2a$mod_label),
                     labels = unique(results_out_h2a$mod_label)) +
    labs(x = "(Actual News - Prior Belief)", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.11,0.27) +
  ggtitle("Difference w/ Priors, Good News")


results_out_h2b <- results_out %>%
  filter(hypothesis=="h2b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","-1","-2","-3","-4")))

h2b <- ggplot(data = results_out_h2b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h2b$mod_label),
                     labels = unique(results_out_h2b$mod_label)) +
    labs(x = "(Actual News - Prior Belief)", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.11,0.27) +
  ggtitle("Difference w/ Priors, Bad News")


results_out_h5a <- results_out %>%
  filter(hypothesis=="h5a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5a <- ggplot(data = results_out_h5a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h5a$mod_label),
                     labels = unique(results_out_h5a$mod_label)) +
    labs(x = "Level of Certainty about Prior", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.12,0.11) +
  ggtitle("Certainty, Good News")

results_out_h5b <- results_out %>%
  filter(hypothesis=="h5b") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5b <- ggplot(data = results_out_h5b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h5b$mod_label),
                     labels = unique(results_out_h5b$mod_label)) +
    labs(x = "Level of Certainty about Prior", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.12,0.11) +
  ggtitle("Certainty, Bad News")


results_out_h6a <- results_out %>%
  filter(hypothesis=="h6a") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6a <- ggplot(data = results_out_h6a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h6a$mod_label),
                     labels = unique(results_out_h6a$mod_label)) +
    labs(x = "Importance of Public Services", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.13,0.22) +
  ggtitle("Importance, Good News")

results_out_h6b <- results_out %>%
  filter(hypothesis=="h6b") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6b <- ggplot(data = results_out_h6b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h6b$mod_label),
                     labels = unique(results_out_h6b$mod_label)) +
    labs(x = "Importance of Public Services", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.13,0.22) +
  ggtitle("Importance, Bad News")


results_out_h8a <- results_out %>%
  filter(hypothesis=="h8a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Not Aligned","Aligned")))

h8a <- ggplot(data = results_out_h8a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h8a$mod_label),
                     labels = unique(results_out_h8a$mod_label)) +
    labs(x = "Alignment w/ Incumbent Chair", 
         y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.035,0.06) +
  ggtitle("Alignment, Good News")

results_out_h8b <- results_out %>%
  filter(hypothesis=="h8b") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h8b <- ggplot(data = results_out_h8b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h8b$mod_label),
                     labels = unique(results_out_h8b$mod_label)) +
    labs(x = "Alignment w/ Incumbent Chair", 
         y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.035,0.06) +
  ggtitle("Alignment, Bad News")


results_out_h9a <- results_out %>%
  filter(hypothesis=="h9a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same tribe", "not same tribe")))

h9a <- ggplot(data = results_out_h9a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h9a$mod_label),
                     labels = unique(results_out_h9a$mod_label)) +
    labs(x = "Same Tribe as LC5 Councillor", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.1,0.059) +
  ggtitle("Clientelist Benefits, Good News")

results_out_h9b <- results_out %>%
  filter(hypothesis=="h9b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same tribe", "not same tribe")))

h9b <- ggplot(data = results_out_h9b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h9b$mod_label),
                     labels = unique(results_out_h9b$mod_label)) +
    labs(x = "Same Tribe as LC5 Councillor", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.1,0.059) +
  ggtitle("Clientelist Benefits, Bad News")


results_out_h10a <- results_out %>%
  filter(hypothesis=="h10a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10a <- ggplot(data = results_out_h10a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h10a$mod_label),
                     labels = unique(results_out_h10a$mod_label)) +
    labs(x = "Receipt of Gifts Prior to Election", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.09,0.036) +
  ggtitle("Vote Buying, Good News")

results_out_h10b <- results_out %>%
  filter(hypothesis=="h10b")  %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10b <- ggplot(data = results_out_h10b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h10b$mod_label),
                     labels = unique(results_out_h10b$mod_label)) +
    labs(x = "Receipt of Gifts Prior to Election", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.09,0.036) +
  ggtitle("Vote Buying, Bad News")

# pdf(file="combined-hte-cov-200611.pdf", height=12, width = 10)
ggarrange(h2a,h2b,h5a,h5b,h6a,h6b,h8a,h8b,h9a,h9b,h10a,h10b,
          ncol = 2, nrow = 6,
          labels = c("A","B","C","D","E","F","G","H","I","J","K","L"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```

## Figure 3: Belief updating about the comparative quality of public services

```{r lc5-updating-estimates, message=FALSE, warning=FALSE, eval=T}

# these are the beliefs at endline, measured at different call centers:
#d.quality_PS_compared.x #LC5
#k.quality_PS_compared.y #Kampala
#sc.quality_PS_compared #LC3

# forming posteriors into numerical rankings for manipulation:
post <- data$d.quality_PS_compared.x 
data$finalps.post <- NA
data$finalps.post <- ifelse(post=="much_worse",1,data$finalps.post)
data$finalps.post <- ifelse(post=="a_little_worse",2,data$finalps.post)
data$finalps.post <- ifelse(post=="don_t_know",3,data$finalps.post)
data$finalps.post <- ifelse(post=="better",4,data$finalps.post)
data$finalps.post <- ifelse(post=="much_better",5,data$finalps.post)

data$finalps.post.diff <-  data$finalps.actual - data$finalps.post

#updating types:
data$manipulation.strict.ps <- ifelse(data$finalps.post.diff==0, 1, 0)

data$manipulation.updating.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) < abs(data$finalps.prior.diff), 1, 0)

data$manipulation.updating.loose.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) <= abs(data$finalps.prior.diff), 1, 0)

data$directional.updating.ps <- ifelse(data$finalps.post.diff==0 & data$finalps.prior.diff==0, 1, 0)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff>0 & data$finalps.post > data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff<0 & data$finalps.post < data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(is.na(data$finalps.prior) | is.na(data$finalps.post), NA, data$directional.updating.ps)

#Accounting for eligibility for divergence
data$diverge.eligble <- 1
data$diverge.eligble <- ifelse(data$finalps.prior==1 & data$finalps.actual>=4, 0, data$diverge.eligble)
data$diverge.eligble <- ifelse(data$finalps.prior==5 & data$finalps.actual<=2, 0, data$diverge.eligble)

data_lc5_first <- data %>% filter(!is.na(finalps.prior.store))
#note: 'finalps.prior.store' is the 'finalps.prior' without imputation
#... when this value is NA subjects were reassigned to a public service that was not their first choice, so we do not have information on their prior or they did not receive information that would allow them to update on their posterior belief, which asked about the service that was most important to them.

##Strict updating estimation:
strict <- felm(manipulation.strict.ps ~ ps.treat |0|0|0, data=data_lc5_first)

strict.fe <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

strict.fe.c <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

##Partial updating estimation:
updating <- felm(manipulation.updating.ps ~ ps.treat |0|0|0, data=data_lc5_first)

updating.fe <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

updating.fe.c <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

##Loose updating estimation:
loose <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=data_lc5_first)

loose.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

loose.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

#Loose updating, eligible estimation:
loose.e <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=filter(data_lc5_first, diverge.eligble==1))

loose.e.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=filter(data_lc5_first, diverge.eligble==1))

loose.e.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=filter(data_lc5_first, diverge.eligble==1))

##Directional updating estimation:
directional <- felm(directional.updating.ps ~ ps.treat |0|0|0, data=data_lc5_first)

directional.fe <- felm(directional.updating.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

directional.fe.c <- felm(directional.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)


##Table harvesting
strict.coef <- c(summary(strict)$coefficient[2,1],summary(strict.fe)$coefficient[1,1],summary(strict.fe.c)$coefficient[1,1])
strict.se <- c(summary(strict)$coefficient[2,2],summary(strict.fe)$coefficient[1,2],summary(strict.fe.c)$coefficient[1,2])
strict.p <- c(summary(strict)$coefficient[2,4],summary(strict.fe)$coefficient[1,4],summary(strict.fe.c)$coefficient[1,4])
strict.p.one.side <- strict.p/2

updating.coef <- c(summary(updating)$coefficient[2,1],summary(updating.fe)$coefficient[1,1],summary(updating.fe.c)$coefficient[1,1])
updating.se <- c(summary(updating)$coefficient[2,2],summary(updating.fe)$coefficient[1,2],summary(updating.fe.c)$coefficient[1,2])
updating.p <- c(summary(updating)$coefficient[2,4],summary(updating.fe)$coefficient[1,4],summary(updating.fe.c)$coefficient[1,4])
updating.p.one.side <- updating.p/2

loose.coef <- c(summary(loose)$coefficient[2,1],summary(loose.fe)$coefficient[1,1],summary(loose.fe.c)$coefficient[1,1])
loose.se <- c(summary(loose)$coefficient[2,2],summary(loose.fe)$coefficient[1,2],summary(loose.fe.c)$coefficient[1,2])
loose.p <- c(summary(loose)$coefficient[2,4],summary(loose.fe)$coefficient[1,4],summary(loose.fe.c)$coefficient[1,4])
loose.p.one.side <- loose.p/2

loose.e.coef <- c(summary(loose.e)$coefficient[2,1],summary(loose.e.fe)$coefficient[1,1],summary(loose.e.fe.c)$coefficient[1,1])
loose.e.se <- c(summary(loose.e)$coefficient[2,2],summary(loose.e.fe)$coefficient[1,2],summary(loose.e.fe.c)$coefficient[1,2])
loose.e.p <- c(summary(loose.e)$coefficient[2,4],summary(loose.e.fe)$coefficient[1,4],summary(loose.e.fe.c)$coefficient[1,4])
loose.e.p.one.side <- loose.e.p/2

directional.coef <- c(summary(directional)$coefficient[2,1],summary(directional.fe)$coefficient[1,1],summary(directional.fe.c)$coefficient[1,1])
directional.se <- c(summary(directional)$coefficient[2,2],summary(directional.fe)$coefficient[1,2],summary(directional.fe.c)$coefficient[1,2])
directional.p <- c(summary(directional)$coefficient[2,4],summary(directional.fe)$coefficient[1,4],summary(directional.fe.c)$coefficient[1,4])
directional.p.one.side <- directional.p/2

##Harvesting for combined figure
level <- c("After LC5 Vote","After LC5 Vote","After LC3 Vote","After LC3 Vote")
updating_type <- c("perfect updating","partial updating","perfect updating","partial updating")
estimate <- NA
se <- NA
updating_fig_dta <- data.frame(level,updating_type,estimate,se)

updating_fig_dta$estimate[1] <- strict.coef
updating_fig_dta$se[1] <- strict.se

updating_fig_dta$estimate[2] <- updating.coef
updating_fig_dta$se[2] <- updating.se

```


```{r lc3-updating-estimates, message=FALSE, warning=FALSE, eval=T}

# these are the beliefs at endline, measured at different call centers:
#d.quality_PS_compared.x #LC5
#k.quality_PS_compared.y #Kampala
#sc.quality_PS_compared #LC3

##Forming posteriors into numerical rankings for manipulation:
post <- ifelse(data$district=="kampala",
               data$k.quality_PS_compared.y,
               data$sc.quality_PS_compared) #merging kampala and other subcounties
  
data$finalps.post <- NA
data$finalps.post <- ifelse(post=="much_worse",1,data$finalps.post)
data$finalps.post <- ifelse(post=="a_little_worse",2,data$finalps.post)
data$finalps.post <- ifelse(post=="don_t_know",3,data$finalps.post)
data$finalps.post <- ifelse(post=="better",4,data$finalps.post)
data$finalps.post <- ifelse(post=="much_better",5,data$finalps.post)

data$finalps.post.diff <-  data$finalps.actual - data$finalps.post


# compiling types of updating:
data$manipulation.strict.ps <- ifelse(data$finalps.post.diff==0, 1, 0)

data$manipulation.updating.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) < abs(data$finalps.prior.diff), 1, 0)

data$manipulation.updating.loose.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) <= abs(data$finalps.prior.diff), 1, 0)

data$directional.updating.ps <- ifelse(data$finalps.post.diff==0 & data$finalps.prior.diff==0, 1, 0)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff>0 & data$finalps.post > data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff<0 & data$finalps.post < data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(is.na(data$finalps.prior) | is.na(data$finalps.post), NA, data$directional.updating.ps)

#Accounting for eligibility for divergence
data$diverge.eligble <- 1
data$diverge.eligble <- ifelse(data$finalps.prior==1 & data$finalps.actual>=4, 0, data$diverge.eligble)
data$diverge.eligble <- ifelse(data$finalps.prior==5 & data$finalps.actual<=2, 0, data$diverge.eligble)

data_lc3_first <- data %>% filter(!is.na(finalps.prior.store))
#note: 'finalps.prior.store' is the 'finalps.prior' without imputation
#... when this value is NA subjects were reassigned to a public service that was not their first choice, so we do not have information on their prior or they did not receive information that would allow them to update on their posterior belief, which asked about the service that was most important to them.

##Strict updating estimation:
strict <- felm(manipulation.strict.ps ~ ps.treat |0|0|0, data=data_lc3_first)

strict.fe <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

strict.fe.c <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Partial updating estimation:
updating <- felm(manipulation.updating.ps ~ ps.treat |0|0|0, data=data_lc3_first)

updating.fe <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

updating.fe.c <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Loose updating estimation:
loose <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=data_lc3_first)

loose.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

loose.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Loose updating, eligible for more divergent beliefs estimation:
loose.e <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=filter(data, diverge.eligble==1))

loose.e.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=filter(data, diverge.eligble==1))

loose.e.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=filter(data, diverge.eligble==1))

##Directional updating estimation:
directional <- felm(directional.updating.ps ~ ps.treat |0|0|0, data=data_lc3_first)

directional.fe <- felm(directional.updating.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

directional.fe.c <- felm(directional.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Table harvesting
strict.coef <- c(summary(strict)$coefficient[2,1],summary(strict.fe)$coefficient[1,1],summary(strict.fe.c)$coefficient[1,1])
strict.se <- c(summary(strict)$coefficient[2,2],summary(strict.fe)$coefficient[1,2],summary(strict.fe.c)$coefficient[1,2])
strict.p <- c(summary(strict)$coefficient[2,4],summary(strict.fe)$coefficient[1,4],summary(strict.fe.c)$coefficient[1,4])
strict.p.one.side <- strict.p/2

updating.coef <- c(summary(updating)$coefficient[2,1],summary(updating.fe)$coefficient[1,1],summary(updating.fe.c)$coefficient[1,1])
updating.se <- c(summary(updating)$coefficient[2,2],summary(updating.fe)$coefficient[1,2],summary(updating.fe.c)$coefficient[1,2])
updating.p <- c(summary(updating)$coefficient[2,4],summary(updating.fe)$coefficient[1,4],summary(updating.fe.c)$coefficient[1,4])
updating.p.one.side <- updating.p/2

loose.coef <- c(summary(loose)$coefficient[2,1],summary(loose.fe)$coefficient[1,1],summary(loose.fe.c)$coefficient[1,1])
loose.se <- c(summary(loose)$coefficient[2,2],summary(loose.fe)$coefficient[1,2],summary(loose.fe.c)$coefficient[1,2])
loose.p <- c(summary(loose)$coefficient[2,4],summary(loose.fe)$coefficient[1,4],summary(loose.fe.c)$coefficient[1,4])
loose.p.one.side <- loose.p/2

loose.e.coef <- c(summary(loose.e)$coefficient[2,1],summary(loose.e.fe)$coefficient[1,1],summary(loose.e.fe.c)$coefficient[1,1])
loose.e.se <- c(summary(loose.e)$coefficient[2,2],summary(loose.e.fe)$coefficient[1,2],summary(loose.e.fe.c)$coefficient[1,2])
loose.e.p <- c(summary(loose.e)$coefficient[2,4],summary(loose.e.fe)$coefficient[1,4],summary(loose.e.fe.c)$coefficient[1,4])
loose.e.p.one.side <- loose.e.p/2

directional.coef <- c(summary(directional)$coefficient[2,1],summary(directional.fe)$coefficient[1,1],summary(directional.fe.c)$coefficient[1,1])
directional.se <- c(summary(directional)$coefficient[2,2],summary(directional.fe)$coefficient[1,2],summary(directional.fe.c)$coefficient[1,2])
directional.p <- c(summary(directional)$coefficient[2,4],summary(directional.fe)$coefficient[1,4],summary(directional.fe.c)$coefficient[1,4])
directional.p.one.side <- directional.p/2

##Harvesting for combined figure:
updating_fig_dta$estimate[3] <- strict.coef
updating_fig_dta$se[3] <- strict.se

updating_fig_dta$estimate[4] <- updating.coef
updating_fig_dta$se[4] <- updating.se

```

```{r figure3, fig.height=4, fig.width=5, eval=T}

updating_fig_dta <- updating_fig_dta %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se) %>%
  mutate(level = factor(level, levels = c("After LC5 Vote", "After LC3 Vote")))

update_fig <- ggplot(data = updating_fig_dta,
            mapping = aes(x = level, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = updating_type, color = updating_type)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(updating_fig_dta$level)) +
    labs(x = "Timing of Measurement",
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = level,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = updating_type, color = updating_type),
                size=1.5, width=0) +
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-.02,0.12) +
  ggtitle("Belief Updating about Public Services") +
  scale_colour_discrete("Updating Type")

# pdf(file="updating-210119.pdf", height=4, width = 5)
update_fig
# dev.off()

```

# Supporting Information

## Figure S1: CONSORT diagram tracking study design

```{r figS1-consort}

#Function to print the table associated with each CONSORT block
PrintTable=function(title){
 
  consort.display=data.frame('Sample Name'=NA, 'Number of Obs'=NA)

  for(i in c(2:length(consort))){
    consort.display[i-1,1]=colnames(consort)[i]
      consort.display[i-1,2]= consort[1,i]
  }
  consort.display %>%
    kbl(caption = title) %>%
    kable_classic(full_width = F, position="left")
 
}

#Setup working data frames
consort.data=data
consort=data.frame(NA)

#Calculate the number of respondents in each enumeration area
consort.data$c=1
num.in.village=aggregate(consort.data$c, by=list(consort.data$location.id), FUN=sum)
consort.data$num.in.village=num.in.village[match(consort.data$location.id,num.in.village$Group.1), "x"]

#Create a data frame excluding cases with missing audit data
consort.data.nona=consort.data[consort.data$ps.bad.noNA==1 | consort.data$ps.good.noNA==1,]

#Create working data frame of those that completed the endline survey
consort.data.surveyed.lc5= consort.data[!is.na(consort.data$d.vote_in_LC5_election.x),]
consort.data.surveyed.lc3= consort.data[!is.na(consort.data$lc3.turnout),]

#Row 1 Column 1
consort$assessed_for_eligibility=30296 #from baseline recruitment files available on request

PrintTable("Assessed for Eligibility at Baseline")
consort=data.frame(NA)

#Row 1 Column 2  
consort$excluded=14213 #from baseline recruitment files available on request
consort$excluded.refused=218 #from baseline recruitment files available on request
consort$excluded.unreachable=13995 #from baseline recruitment files available on request

PrintTable("Excluded")
consort=data.frame(NA)

#Row 2 Column 1  - Number Randomized
consort$randomized=nrow(consort.data)

PrintTable("Number Randomized")
consort=data.frame(NA)

#Row 3 Column 1 - Public Services Treatment
consort$treatment=sum(consort.data$ps.treat==1 & !is.na(consort.data$ps.treat))
consort$treatment_roads=sum(consort.data$finalps=="roads.1")
consort$treatment_water=sum(consort.data$finalps=="water.1")
consort$treatment_health=sum(consort.data$finalps=="health.1")
consort$treatment_education=sum(consort.data$finalps=="education.1")

PrintTable("Public Services Treatment Assignments")
consort=data.frame(NA)

#Row 3 Column 2 - Placebo Treatment
consort$placebo=sum(consort.data$ps.treat==0 & !is.na(consort.data$ps.treat))
consort$placebo_roads=sum(consort.data$finalps=="roads.0")
consort$placebo_water=sum(consort.data$finalps=="water.0")
consort$placebo_health=sum(consort.data$finalps=="health.0")
consort$placebo_education=sum(consort.data$finalps=="education.0")

PrintTable("Public Services Placebo Assignments")
consort=data.frame(NA)

#Row 4 Column 1 - Surveyed at endline in treatment group
consort$treatment_surveyed_endline_lc5=sum(!is.na(consort.data[consort.data$ps.treat==1 & !is.na(consort.data$ps.treat),]$d.vote_in_LC5_election.x))
consort$treatment_lc5_unreachable = sum(is.na(consort.data[consort.data$ps.treat==1 & !is.na(consort.data$ps.treat),]$d.vote_in_LC5_election.x))
consort$treatment_surveyed_endline_lc3=sum(!is.na(consort.data[consort.data$ps.treat==1,]$lc3.turnout))
consort$treatment_lc3_unreachable = sum(is.na(consort.data[consort.data$ps.treat==1,]$lc3.turnout))

PrintTable("Number Surveyed at Endline in Treatment Group")
consort=data.frame(NA)

#Row 4 Column 2 - Surveyed at endline in placebo group
consort$placebo_surveyed_endline_lc5=sum(!is.na(consort.data[consort.data$ps.treat==0 ,]$d.vote_in_LC5_election.x))
consort$placebo_lc5_unreachable = sum(is.na(consort.data[consort.data$ps.treat==0 & !is.na(consort.data$ps.treat),]$d.vote_in_LC5_election.x))
consort$placebo_surveyed_endline_lc3=sum(!is.na(consort.data[consort.data$ps.treat==0,]$lc3.turnout))
consort$placebo_lc3_unreachable = sum(is.na(consort.data[consort.data$ps.treat==0,]$lc3.turnout))

PrintTable("Number Surveyed at Endline in Placebo Group")
consort=data.frame(NA)

#Row 5 Column 1 - Number analyzed and reasons for exclusion in treatment

#Number Analyzed 
consort$treat.analysed.lc5.chair=nrow(filter(consort.data.nona,  lc5.chair.competitive==1 & lc5.chair.party.switch==0 & !is.na(lc5.chair.inc.vote) & !is.na(lc5.chair.intent) & ps.treat==1))

consort$treat.analysed.lc5.councillor=nrow(filter(consort.data.nona,  lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & !is.na(lc5.councillor.inc.vote) & !is.na(lc5.councillor.intent) & (ps.treat==1) & is.na(lc5.councillor.redistricted2016)))

consort$treat.analysed.lc3.chair=nrow(filter(consort.data.nona, lc3.chair.competitive==1 & lc3.chair.party.switch==0 & !is.na(lc3.chair.inc.vote) & !is.na(lc3.chair.intent) & (ps.treat==1) & is.na(lc3.chair.redistricted)))

consort$treat.analysed.lc3.councillor=nrow(filter(consort.data.nona, lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & !is.na(lc3.councillor.inc.vote) & !is.na(lc3.councillor.intent) & (ps.treat==1) & is.na(lc3.councillor.redistricted2016)))

PrintTable("Number Analyzed in Treatment Group")
consort=data.frame(NA)

#Reasons for exclusion
#Number of cases with missing audit data
excluded.lc5=filter(consort.data.surveyed.lc5, (ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==1)
excluded.lc3=filter(consort.data.surveyed.lc3, (ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==1)

included.lc5=filter(consort.data.surveyed.lc5, !(ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==1)
included.lc3=filter(consort.data.surveyed.lc3, !(ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==1)

consort$treat.analysed.lc5.noaudit=nrow(excluded.lc5)
consort$treat.analysed.lc3.noaudit=nrow(excluded.lc3)

#Cases in uncompetitive elections
excluded.lc5.chair=(filter(included.lc5,  !(lc5.chair.competitive==1) ))
excluded.lc5.councillor=(filter(included.lc5,  !(lc5.councillor.competitive==1) ))
excluded.lc3.chair=(filter(included.lc3,  !(lc3.chair.competitive==1)))
excluded.lc3.councillor=(filter(included.lc3,  !(lc3.councillor.competitive==1) ))

included.lc5.chair=(filter(included.lc5,  (lc5.chair.competitive==1)))
included.lc5.councillor=(filter(included.lc5,  (lc5.councillor.competitive==1)))
included.lc3.chair=(filter(included.lc3,  (lc3.chair.competitive==1) ))
included.lc3.councillor=(filter(included.lc3,  (lc3.councillor.competitive==1)))

consort$treat.analysed.lc5.chair.notcompetitive=nrow(excluded.lc5.chair)
consort$treat.analysed.lc5.councillor.notcompetitive=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.notcompetitive=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.notcompetitive=nrow(excluded.lc3.councillor)

#Cases where incumbent contested another seat
excluded.lc5.councillor=filter(included.lc5.councillor,  !is.na(lc5.councillor.redistricted2016) )
excluded.lc3.chair=filter(included.lc3.chair, !is.na(lc3.chair.redistricted) )
excluded.lc3.councillor=filter(included.lc3.councillor, !is.na(lc3.councillor.redistricted2016) )

included.lc5.councillor=filter(included.lc5.councillor,  is.na(lc5.councillor.redistricted2016) )
included.lc3.chair=filter(included.lc3.chair, is.na(lc3.chair.redistricted) )
included.lc3.councillor=filter(included.lc3.councillor, is.na(lc3.councillor.redistricted2016) )

consort$treat.analysed.lc5.councillor.redistricted=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.redistricted=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.redistricted=nrow(excluded.lc3.councillor)

#Cases where incumbent switched parties
excluded.lc5.chair=filter(included.lc5.chair,  !(lc5.chair.party.switch==0) )
excluded.lc5.councillor=filter(included.lc5.councillor,  !(lc5.councillor.party.switch==0) )
excluded.lc3.chair=filter(included.lc3.chair, !(lc3.chair.party.switch==0) )
excluded.lc3.councillor=filter(included.lc3.councillor, !(lc3.councillor.party.switch==0) )

included.lc5.chair=filter(included.lc5.chair,  (lc5.chair.party.switch==0))
included.lc5.councillor=filter(included.lc5.councillor,  (lc5.councillor.party.switch==0))
included.lc3.chair=filter(included.lc3.chair, (lc3.chair.party.switch==0) )
included.lc3.councillor=filter(included.lc3.councillor, (lc3.councillor.party.switch==0) )

consort$treat.analysed.lc5.chair.partyswitch=nrow(excluded.lc5.chair)
consort$treat.analysed.lc5.councillor.partyswitch=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.partyswitch=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.partyswitch=nrow(excluded.lc3.councillor)

#Cases where respondent refused vote intention question
excluded.lc5.chair=filter(included.lc5.chair,  is.na(lc5.chair.intent) )
excluded.lc5.councillor=filter(included.lc5.councillor,   is.na(lc5.councillor.intent) )
excluded.lc3.chair=filter(included.lc3.chair,  is.na(lc3.chair.intent) )
excluded.lc3.councillor=filter(included.lc3.councillor,  is.na(lc3.councillor.intent) )

included.lc5.chair=filter(included.lc5.chair,  !is.na(lc5.chair.intent) )
included.lc5.councillor=filter(included.lc5.councillor,   !is.na(lc5.councillor.intent) )
included.lc3.chair=filter(included.lc3.chair,  !is.na(lc3.chair.intent) )
included.lc3.councillor=filter(included.lc3.councillor,  !is.na(lc3.councillor.intent) )

consort$treat.analysed.lc5.chair.nointent=nrow(excluded.lc5.chair)
consort$treat.analysed.lc5.councillor.nointent=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.nointent=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.nointent=nrow(excluded.lc3.councillor)

#Cases where respondent did not admit to voting
excluded.lc5.chair=filter(included.lc5.chair,  !(lc5.turnout==1) )
excluded.lc5.councillor=filter(included.lc5.councillor,  !(lc5.turnout==1) )
excluded.lc3.chair=filter(included.lc3.chair, !(lc3.turnout==1) )
excluded.lc3.councillor=filter(included.lc3.councillor, !(lc3.turnout==1) )

included.lc5.chair=filter(included.lc5.chair,  (lc5.turnout==1))
included.lc5.councillor=filter(included.lc5.councillor,  (lc5.turnout==1))
included.lc3.chair=filter(included.lc3.chair, (lc3.turnout==1) )
included.lc3.councillor=filter(included.lc3.councillor, (lc3.turnout==1) )

consort$treat.analysed.lc5.chair.noturnout=nrow(excluded.lc5.chair)
consort$treat.analysed.lc5.councillor.noturnout=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.noturnout=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.noturnout=nrow(excluded.lc3.councillor)

#Cases where respondent refused vote choice
excluded.lc5.chair=filter(included.lc5.chair,  is.na(lc5.chair.inc.vote))
excluded.lc5.councillor=filter(included.lc5.councillor,  is.na(lc5.councillor.inc.vote))
excluded.lc3.chair=filter(included.lc3.chair,  is.na(lc3.chair.inc.vote))
excluded.lc3.councillor=filter(included.lc3.councillor,  is.na(lc3.councillor.inc.vote))

included.lc5.chair=filter(included.lc5.chair,  !is.na(lc5.chair.inc.vote))
included.lc5.councillor=filter(included.lc5.councillor,  !is.na(lc5.councillor.inc.vote))
included.lc3.chair=filter(included.lc3.chair,  !is.na(lc3.chair.inc.vote))
included.lc3.councillor=filter(included.lc3.councillor,  !is.na(lc3.councillor.inc.vote))

consort$treat.analysed.lc5.chair.refusedvote=nrow(excluded.lc5.chair)
consort$treat.analysed.lc5.councillor.refusedvote=nrow(excluded.lc5.councillor)
consort$treat.analysed.lc3.chair.refusedvote=nrow(excluded.lc3.chair)
consort$treat.analysed.lc3.councillor.refusedvote=nrow(excluded.lc3.councillor)

PrintTable("Number Excluded in Treatment Group")
consort=data.frame(NA)

#Row 5 Column 2 - Number analyzed and reasons for exclusion in placebo

# Number Analyzed 
consort$placebo.analysed.lc5.chair=nrow(filter(consort.data.nona,  lc5.chair.competitive==1 & lc5.chair.party.switch==0 & !is.na(lc5.chair.inc.vote) & !is.na(lc5.chair.intent) & ps.treat==0))

consort$placebo.analysed.lc5.councillor=nrow(filter(consort.data.nona,  lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & !is.na(lc5.councillor.inc.vote) & !is.na(lc5.councillor.intent) & (ps.treat==0) & is.na(lc5.councillor.redistricted2016)))

consort$placebo.analysed.lc3.chair=nrow(filter(consort.data.nona, lc3.chair.competitive==1 & lc3.chair.party.switch==0 & !is.na(lc3.chair.inc.vote) & !is.na(lc3.chair.intent) & (ps.treat==0) & is.na(lc3.chair.redistricted)))

consort$placebo.analysed.lc3.councillor=nrow(filter(consort.data.nona, lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & !is.na(lc3.councillor.inc.vote) & !is.na(lc3.councillor.intent) & (ps.treat==0) & is.na(lc3.councillor.redistricted2016)))

PrintTable("Number Analyzed in Placebo Group")
consort=data.frame(NA)

#Reasons for exclusion
#Number of cases with missing audit data
excluded.lc5=filter(consort.data.surveyed.lc5, (ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==0)
excluded.lc3=filter(consort.data.surveyed.lc3, (ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==0)

included.lc5=filter(consort.data.surveyed.lc5, !(ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==0)
included.lc3=filter(consort.data.surveyed.lc3, !(ps.bad.noNA==0 & ps.good.noNA==0) & ps.treat==0)

consort$placebo.analysed.lc5.noaudit=nrow(excluded.lc5)
consort$placebo.analysed.lc3.noaudit=nrow(excluded.lc3)

#Cases with uncompetitive elections
excluded.lc5.chair=(filter(included.lc5,  !(lc5.chair.competitive==1) ))
excluded.lc5.councillor=(filter(included.lc5,  !(lc5.councillor.competitive==1) ))
excluded.lc3.chair=(filter(included.lc3,  !(lc3.chair.competitive==1)))
excluded.lc3.councillor=(filter(included.lc3,  !(lc3.councillor.competitive==1) ))

included.lc5.chair=(filter(included.lc5,  (lc5.chair.competitive==1)))
included.lc5.councillor=(filter(included.lc5,  (lc5.councillor.competitive==1)))
included.lc3.chair=(filter(included.lc3,  (lc3.chair.competitive==1) ))
included.lc3.councillor=(filter(included.lc3,  (lc3.councillor.competitive==1)))

consort$placebo.analysed.lc5.chair.notcompetitive=nrow(excluded.lc5.chair)
consort$placebo.analysed.lc5.councillor.notcompetitive=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.notcompetitive=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.notcompetitive=nrow(excluded.lc3.councillor)

#Cases where incumbent contested another seat
excluded.lc5.councillor=filter(included.lc5.councillor,  !is.na(lc5.councillor.redistricted2016) )
excluded.lc3.chair=filter(included.lc3.chair, !is.na(lc3.chair.redistricted) )
excluded.lc3.councillor=filter(included.lc3.councillor, !is.na(lc3.councillor.redistricted2016) )

included.lc5.councillor=filter(included.lc5.councillor,  is.na(lc5.councillor.redistricted2016) )
included.lc3.chair=filter(included.lc3.chair, is.na(lc3.chair.redistricted) )
included.lc3.councillor=filter(included.lc3.councillor, is.na(lc3.councillor.redistricted2016) )

consort$placebo.analysed.lc5.councillor.redistricted=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.redistricted=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.redistricted=nrow(excluded.lc3.councillor)

#Cases where incumbent switched parties
excluded.lc5.chair=filter(included.lc5.chair,  !(lc5.chair.party.switch==0) )
excluded.lc5.councillor=filter(included.lc5.councillor,  !(lc5.councillor.party.switch==0) )
excluded.lc3.chair=filter(included.lc3.chair, !(lc3.chair.party.switch==0) )
excluded.lc3.councillor=filter(included.lc3.councillor, !(lc3.councillor.party.switch==0) )

included.lc5.chair=filter(included.lc5.chair,  (lc5.chair.party.switch==0))
included.lc5.councillor=filter(included.lc5.councillor,  (lc5.councillor.party.switch==0))
included.lc3.chair=filter(included.lc3.chair, (lc3.chair.party.switch==0) )
included.lc3.councillor=filter(included.lc3.councillor, (lc3.councillor.party.switch==0) )

consort$placebo.analysed.lc5.chair.partyswitch=nrow(excluded.lc5.chair)
consort$placebo.analysed.lc5.councillor.partyswitch=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.partyswitch=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.partyswitch=nrow(excluded.lc3.councillor)

#Cases where respondent refused vote intention question at baseline
excluded.lc5.chair=filter(included.lc5.chair,  is.na(lc5.chair.intent) )
excluded.lc5.councillor=filter(included.lc5.councillor,   is.na(lc5.councillor.intent) )
excluded.lc3.chair=filter(included.lc3.chair,  is.na(lc3.chair.intent) )
excluded.lc3.councillor=filter(included.lc3.councillor,  is.na(lc3.councillor.intent) )

included.lc5.chair=filter(included.lc5.chair,  !is.na(lc5.chair.intent) )
included.lc5.councillor=filter(included.lc5.councillor,   !is.na(lc5.councillor.intent) )
included.lc3.chair=filter(included.lc3.chair,  !is.na(lc3.chair.intent) )
included.lc3.councillor=filter(included.lc3.councillor,  !is.na(lc3.councillor.intent) )

consort$placebo.analysed.lc5.chair.nointent=nrow(excluded.lc5.chair)
consort$placebo.analysed.lc5.councillor.nointent=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.nointent=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.nointent=nrow(excluded.lc3.councillor)

#Cases where respondent did not admit to voting
excluded.lc5.chair=filter(included.lc5.chair,  !(lc5.turnout==1) )
excluded.lc5.councillor=filter(included.lc5.councillor,  !(lc5.turnout==1) )
excluded.lc3.chair=filter(included.lc3.chair, !(lc3.turnout==1) )
excluded.lc3.councillor=filter(included.lc3.councillor, !(lc3.turnout==1) )

included.lc5.chair=filter(included.lc5.chair,  (lc5.turnout==1))
included.lc5.councillor=filter(included.lc5.councillor,  (lc5.turnout==1))
included.lc3.chair=filter(included.lc3.chair, (lc3.turnout==1) )
included.lc3.councillor=filter(included.lc3.councillor, (lc3.turnout==1) )

consort$placebo.analysed.lc5.chair.noturnout=nrow(excluded.lc5.chair)
consort$placebo.analysed.lc5.councillor.noturnout=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.noturnout=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.noturnout=nrow(excluded.lc3.councillor)

#Cases where respondent refused the vote choice question at endline
excluded.lc5.chair=filter(included.lc5.chair,  is.na(lc5.chair.inc.vote))
excluded.lc5.councillor=filter(included.lc5.councillor,  is.na(lc5.councillor.inc.vote))
excluded.lc3.chair=filter(included.lc3.chair,  is.na(lc3.chair.inc.vote))
excluded.lc3.councillor=filter(included.lc3.councillor,  is.na(lc3.councillor.inc.vote))

included.lc5.chair=filter(included.lc5.chair,  !is.na(lc5.chair.inc.vote))
included.lc5.councillor=filter(included.lc5.councillor,  !is.na(lc5.councillor.inc.vote))
included.lc3.chair=filter(included.lc3.chair,  !is.na(lc3.chair.inc.vote))
included.lc3.councillor=filter(included.lc3.councillor,  !is.na(lc3.councillor.inc.vote))

consort$placebo.analysed.lc5.chair.refusedvote=nrow(excluded.lc5.chair)
consort$placebo.analysed.lc5.councillor.refusedvote=nrow(excluded.lc5.councillor)
consort$placebo.analysed.lc3.chair.refusedvote=nrow(excluded.lc3.chair)
consort$placebo.analysed.lc3.councillor.refusedvote=nrow(excluded.lc3.councillor)

PrintTable("Number Excluded in Placebo Group")
consort=data.frame(NA)

```


## Figure S4: Descriptive data from post-experiment survey on subject experiences with SMS messages

```{r figS4-experience-survey, fig.height=7, fig.width = 6.5}

#Remember messages from Twaweza:
dta <- sms_survey %>% 
  group_by(`3_Do_you_remember_twaweza_messages`) %>%
  summarise(count=n()) %>%
  rename(remember=`3_Do_you_remember_twaweza_messages`)

remember <- ggplot(dta,aes(x=remember, y=count)) + 
  geom_bar(stat = "identity") +
  xlab('Remember messages from Twaweza?')

#Easy to distinguish:
dta <- sms_survey %>%
  mutate(easy=case_when(`5_How_easy_distinguish_twaweza` %in%
                          c("very_difficult","difficult") ~ "difficult",
                        `5_How_easy_distinguish_twaweza` %in%
                          c("don_t_know","neither_easy_o") ~ "neither",
                        `5_How_easy_distinguish_twaweza` %in%
                          c("easy","very_easy") ~ "easy")) %>%
  group_by(easy) %>%
  summarise(count=n()) %>%
  filter(!is.na(easy)) %>%
  mutate(easy = factor(easy, levels = c("difficult","neither","easy")))

easy <- ggplot(dta,aes(x=easy, y=count)) + 
  geom_bar(stat = "identity") +
  xlab('Easy to distinguish Twaweza messages?')

##Easy to understand:
dta <- sms_survey %>%
  mutate(understand=case_when(
    `8_How_difficult_understanding_messages` %in%
      c("difficult") ~ "difficult",
    `8_How_difficult_understanding_messages` %in%
      c("don_t_know","neither_diffic") ~ "neither",
    `8_How_difficult_understanding_messages` %in%
      c("easy","very_easy") ~ "easy")) %>%
  group_by(understand) %>%
  summarise(count=n()) %>%
  filter(!is.na(understand)) %>%
  mutate(understand = factor(understand, 
                             levels = c("difficult","neither","easy")))

understand <- ggplot(dta,aes(x=understand, y=count)) + 
  geom_bar(stat = "identity") +
  xlab('Easy to understand Twaweza messages?')

##Messages valuable:
dta <- sms_survey %>%
  mutate(value=case_when(
    `9_How_valuable_were_twaweza_messages` %in%
      c("not_valuable_a") ~ "not at all",
    `9_How_valuable_were_twaweza_messages` %in%
      c("not_very_valua") ~ "not very",
    `9_How_valuable_were_twaweza_messages` %in%
      c("somewhat_valua") ~ "somewhat",
    `9_How_valuable_were_twaweza_messages` %in%
      c("very_valuable") ~ "very")) %>%
  group_by(value) %>%
  summarise(count=n()) %>%
  filter(!is.na(value)) %>%
  mutate(value = factor(value, 
                        levels = 
                          c("not at all","not very","somewhat","very")))

valuable <- ggplot(dta,aes(x=value, y=count)) + 
  geom_bar(stat = "identity") +
  xlab('How valuable were Twaweza messages?')

#pdf(file="sms-experience-191125.pdf", height=7, width = 6.5)
plot_grid(remember,easy,understand, valuable,
          labels="AUTO", ncol=2)
#dev.off()

```

## Table S4: Evidence of subject belief updating at the LC5 endline survey

```{r tabS4-belief-updating-lc5, eval=T}

#Note: this chunk harvests re-runs code already compiled for Figure 3 (code chunk `lc5-updating-esetimates`) above so that it can be run independently.

# these are the beliefs at endline, measured at different call centers:
#d.quality_PS_compared.x #LC5
#k.quality_PS_compared.y #Kampala
#sc.quality_PS_compared #LC3

# forming posteriors into numerical rankings for manipulation:
post <- data$d.quality_PS_compared.x 
data$finalps.post <- NA
data$finalps.post <- ifelse(post=="much_worse",1,data$finalps.post)
data$finalps.post <- ifelse(post=="a_little_worse",2,data$finalps.post)
data$finalps.post <- ifelse(post=="don_t_know",3,data$finalps.post)
data$finalps.post <- ifelse(post=="better",4,data$finalps.post)
data$finalps.post <- ifelse(post=="much_better",5,data$finalps.post)

data$finalps.post.diff <-  data$finalps.actual - data$finalps.post

#updating types:
data$manipulation.strict.ps <- ifelse(data$finalps.post.diff==0, 1, 0)

data$manipulation.updating.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) < abs(data$finalps.prior.diff), 1, 0)

data$manipulation.updating.loose.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) <= abs(data$finalps.prior.diff), 1, 0)

data$directional.updating.ps <- ifelse(data$finalps.post.diff==0 & data$finalps.prior.diff==0, 1, 0)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff>0 & data$finalps.post > data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff<0 & data$finalps.post < data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(is.na(data$finalps.prior) | is.na(data$finalps.post), NA, data$directional.updating.ps)

#Accounting for eligibility for divergence
data$diverge.eligble <- 1
data$diverge.eligble <- ifelse(data$finalps.prior==1 & data$finalps.actual>=4, 0, data$diverge.eligble)
data$diverge.eligble <- ifelse(data$finalps.prior==5 & data$finalps.actual<=2, 0, data$diverge.eligble)

data_lc5_first <- data %>% filter(!is.na(finalps.prior.store))
#note: 'finalps.prior.store' is the 'finalps.prior' without imputation
#... when this value is NA subjects were reassigned to a public service that was not their first choice, so we do not have information on their prior or they did not receive information that would allow them to update on their posterior belief, which asked about the service that was most important to them.

##Strict updating estimation:
strict <- felm(manipulation.strict.ps ~ ps.treat |0|0|0, data=data_lc5_first)

strict.fe <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

strict.fe.c <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

##Partial updating estimation:
updating <- felm(manipulation.updating.ps ~ ps.treat |0|0|0, data=data_lc5_first)

updating.fe <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

updating.fe.c <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

##Loose updating estimation:
loose <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=data_lc5_first)

loose.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

loose.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

#Loose updating, eligible estimation:
loose.e <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=filter(data_lc5_first, diverge.eligble==1))

loose.e.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=filter(data_lc5_first, diverge.eligble==1))

loose.e.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=filter(data_lc5_first, diverge.eligble==1))

##Directional updating estimation:
directional <- felm(directional.updating.ps ~ ps.treat |0|0|0, data=data_lc5_first)

directional.fe <- felm(directional.updating.ps ~ ps.treat |location.id|0|0, data=data_lc5_first)

directional.fe.c <- felm(directional.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc5_first)

##Table harvesting
strict.coef <- c(summary(strict)$coefficient[2,1],summary(strict.fe)$coefficient[1,1],summary(strict.fe.c)$coefficient[1,1])
strict.se <- c(summary(strict)$coefficient[2,2],summary(strict.fe)$coefficient[1,2],summary(strict.fe.c)$coefficient[1,2])
strict.p <- c(summary(strict)$coefficient[2,4],summary(strict.fe)$coefficient[1,4],summary(strict.fe.c)$coefficient[1,4])
strict.p.one.side <- strict.p/2

updating.coef <- c(summary(updating)$coefficient[2,1],summary(updating.fe)$coefficient[1,1],summary(updating.fe.c)$coefficient[1,1])
updating.se <- c(summary(updating)$coefficient[2,2],summary(updating.fe)$coefficient[1,2],summary(updating.fe.c)$coefficient[1,2])
updating.p <- c(summary(updating)$coefficient[2,4],summary(updating.fe)$coefficient[1,4],summary(updating.fe.c)$coefficient[1,4])
updating.p.one.side <- updating.p/2

loose.coef <- c(summary(loose)$coefficient[2,1],summary(loose.fe)$coefficient[1,1],summary(loose.fe.c)$coefficient[1,1])
loose.se <- c(summary(loose)$coefficient[2,2],summary(loose.fe)$coefficient[1,2],summary(loose.fe.c)$coefficient[1,2])
loose.p <- c(summary(loose)$coefficient[2,4],summary(loose.fe)$coefficient[1,4],summary(loose.fe.c)$coefficient[1,4])
loose.p.one.side <- loose.p/2

loose.e.coef <- c(summary(loose.e)$coefficient[2,1],summary(loose.e.fe)$coefficient[1,1],summary(loose.e.fe.c)$coefficient[1,1])
loose.e.se <- c(summary(loose.e)$coefficient[2,2],summary(loose.e.fe)$coefficient[1,2],summary(loose.e.fe.c)$coefficient[1,2])
loose.e.p <- c(summary(loose.e)$coefficient[2,4],summary(loose.e.fe)$coefficient[1,4],summary(loose.e.fe.c)$coefficient[1,4])
loose.e.p.one.side <- loose.e.p/2

directional.coef <- c(summary(directional)$coefficient[2,1],summary(directional.fe)$coefficient[1,1],summary(directional.fe.c)$coefficient[1,1])
directional.se <- c(summary(directional)$coefficient[2,2],summary(directional.fe)$coefficient[1,2],summary(directional.fe.c)$coefficient[1,2])
directional.p <- c(summary(directional)$coefficient[2,4],summary(directional.fe)$coefficient[1,4],summary(directional.fe.c)$coefficient[1,4])
directional.p.one.side <- directional.p/2

#Formatting functions, for combined table
coef.print <- function(vector){
  sprintf("%.3f",round(vector,3))
}

se.print <- function(vector){
  paste("(",sprintf("%.3f",round(vector,3)),")",sep="")
}

p.print <- function(vector){
  paste("p=",sprintf("%.3f", round(vector,3)),sep="")
}

updating.type.lab <- c("Strict","","","Updating","","","Loose Updating","","","Loose Updating Eligible","","","Directional","","")

tab.dta <- rbind(coef.print(strict.coef),se.print(strict.se),p.print(strict.p),
                 coef.print(updating.coef),se.print(updating.se),p.print(updating.p),
                 coef.print(loose.coef),se.print(loose.se),p.print(loose.p),
                 coef.print(loose.e.coef),se.print(loose.e.se),p.print(loose.e.p),
                 coef.print(directional.coef),se.print(directional.se),p.print(directional.p))
tab.dta <- cbind(updating.type.lab,tab.dta)
stargazer(tab.dta, type="html", title="LC5 Endline", notes = "Two-sided p-values")
# stargazer(tab.dta, type="latex", title="LC5 Endline", notes = "Two-sided p-values") 
#Date and time: Tue, Jan 05, 2021 - 15:17:31

```

## Table S5: Evidence of subject belief updating at the LC3 endline survey

```{r tabS5-belief-updating-lc3, eval=T}

#Note: this chunk harvests re-runs code already compiled for Figure 3 (code chunk `lc5-updating-esetimates`) above so that it can be run independently.

# these are the beliefs at endline, measured at different call centers:
#d.quality_PS_compared.x #LC5
#k.quality_PS_compared.y #Kampala
#sc.quality_PS_compared #LC3

# these are the beliefs at endline, measured at different call centers:
#d.quality_PS_compared.x #LC5
#k.quality_PS_compared.y #Kampala
#sc.quality_PS_compared #LC3

##Forming posteriors into numerical rankings for manipulation:
post <- ifelse(data$district=="kampala",
               data$k.quality_PS_compared.y,
               data$sc.quality_PS_compared) #merging kampala and other subcounties

data$finalps.post <- NA
data$finalps.post <- ifelse(post=="much_worse",1,data$finalps.post)
data$finalps.post <- ifelse(post=="a_little_worse",2,data$finalps.post)
data$finalps.post <- ifelse(post=="don_t_know",3,data$finalps.post)
data$finalps.post <- ifelse(post=="better",4,data$finalps.post)
data$finalps.post <- ifelse(post=="much_better",5,data$finalps.post)

data$finalps.post.diff <-  data$finalps.actual - data$finalps.post

# compiling types of updating:
data$manipulation.strict.ps <- ifelse(data$finalps.post.diff==0, 1, 0)

data$manipulation.updating.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) < abs(data$finalps.prior.diff), 1, 0)

data$manipulation.updating.loose.ps <- ifelse(data$finalps.post.diff==0 | abs(data$finalps.post.diff) <= abs(data$finalps.prior.diff), 1, 0)

data$directional.updating.ps <- ifelse(data$finalps.post.diff==0 & data$finalps.prior.diff==0, 1, 0)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff>0 & data$finalps.post > data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(data$finalps.prior.diff<0 & data$finalps.post < data$finalps.prior, 1, data$directional.updating.ps)
data$directional.updating.ps <- ifelse(is.na(data$finalps.prior) | is.na(data$finalps.post), NA, data$directional.updating.ps)

#Accounting for eligibility for divergence
data$diverge.eligble <- 1
data$diverge.eligble <- ifelse(data$finalps.prior==1 & data$finalps.actual>=4, 0, data$diverge.eligble)
data$diverge.eligble <- ifelse(data$finalps.prior==5 & data$finalps.actual<=2, 0, data$diverge.eligble)

data_lc3_first <- data %>% filter(!is.na(finalps.prior.store))
#note: 'finalps.prior.store' is the 'finalps.prior' without imputation
#... when this value is NA subjects were reassigned to a public service that was not their first choice, so we do not have information on their prior or they did not receive information that would allow them to update on their posterior belief, which asked about the service that was most important to them.

##Strict updating estimation:
strict <- felm(manipulation.strict.ps ~ ps.treat |0|0|0, data=data_lc3_first)

strict.fe <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

strict.fe.c <- felm(manipulation.strict.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Partial updating estimation:
updating <- felm(manipulation.updating.ps ~ ps.treat |0|0|0, data=data_lc3_first)

updating.fe <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

updating.fe.c <- felm(manipulation.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Loose updating estimation:
loose <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=data_lc3_first)

loose.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

loose.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Loose updating, eligible for more divergent beliefs estimation:
loose.e <- felm(manipulation.updating.loose.ps ~ ps.treat |0|0|0, data=filter(data, diverge.eligble==1))

loose.e.fe <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|0, data=filter(data, diverge.eligble==1))

loose.e.fe.c <- felm(manipulation.updating.loose.ps ~ ps.treat |location.id|0|district_lower, data=filter(data, diverge.eligble==1))

##Directional updating estimation:
directional <- felm(directional.updating.ps ~ ps.treat |0|0|0, data=data_lc3_first)

directional.fe <- felm(directional.updating.ps ~ ps.treat |location.id|0|0, data=data_lc3_first)

directional.fe.c <- felm(directional.updating.ps ~ ps.treat |location.id|0|district_lower, data=data_lc3_first)

##Table harvesting
strict.coef <- c(summary(strict)$coefficient[2,1],summary(strict.fe)$coefficient[1,1],summary(strict.fe.c)$coefficient[1,1])
strict.se <- c(summary(strict)$coefficient[2,2],summary(strict.fe)$coefficient[1,2],summary(strict.fe.c)$coefficient[1,2])
strict.p <- c(summary(strict)$coefficient[2,4],summary(strict.fe)$coefficient[1,4],summary(strict.fe.c)$coefficient[1,4])
strict.p.one.side <- strict.p/2

updating.coef <- c(summary(updating)$coefficient[2,1],summary(updating.fe)$coefficient[1,1],summary(updating.fe.c)$coefficient[1,1])
updating.se <- c(summary(updating)$coefficient[2,2],summary(updating.fe)$coefficient[1,2],summary(updating.fe.c)$coefficient[1,2])
updating.p <- c(summary(updating)$coefficient[2,4],summary(updating.fe)$coefficient[1,4],summary(updating.fe.c)$coefficient[1,4])
updating.p.one.side <- updating.p/2

loose.coef <- c(summary(loose)$coefficient[2,1],summary(loose.fe)$coefficient[1,1],summary(loose.fe.c)$coefficient[1,1])
loose.se <- c(summary(loose)$coefficient[2,2],summary(loose.fe)$coefficient[1,2],summary(loose.fe.c)$coefficient[1,2])
loose.p <- c(summary(loose)$coefficient[2,4],summary(loose.fe)$coefficient[1,4],summary(loose.fe.c)$coefficient[1,4])
loose.p.one.side <- loose.p/2

loose.e.coef <- c(summary(loose.e)$coefficient[2,1],summary(loose.e.fe)$coefficient[1,1],summary(loose.e.fe.c)$coefficient[1,1])
loose.e.se <- c(summary(loose.e)$coefficient[2,2],summary(loose.e.fe)$coefficient[1,2],summary(loose.e.fe.c)$coefficient[1,2])
loose.e.p <- c(summary(loose.e)$coefficient[2,4],summary(loose.e.fe)$coefficient[1,4],summary(loose.e.fe.c)$coefficient[1,4])
loose.e.p.one.side <- loose.e.p/2

directional.coef <- c(summary(directional)$coefficient[2,1],summary(directional.fe)$coefficient[1,1],summary(directional.fe.c)$coefficient[1,1])
directional.se <- c(summary(directional)$coefficient[2,2],summary(directional.fe)$coefficient[1,2],summary(directional.fe.c)$coefficient[1,2])
directional.p <- c(summary(directional)$coefficient[2,4],summary(directional.fe)$coefficient[1,4],summary(directional.fe.c)$coefficient[1,4])
directional.p.one.side <- directional.p/2

updating.type.lab <- c("Strict","","","Updating","","","Loose Updating","","","Loose Updating Eligible","","","Directional","","")

tab.dta <- rbind(coef.print(strict.coef),se.print(strict.se),p.print(strict.p),
                 coef.print(updating.coef),se.print(updating.se),p.print(updating.p),
                 coef.print(loose.coef),se.print(loose.se),p.print(loose.p),
                 coef.print(loose.e.coef),se.print(loose.e.se),p.print(loose.e.p),
                 coef.print(directional.coef),se.print(directional.se),p.print(directional.p))
tab.dta <- cbind(updating.type.lab,tab.dta)
stargazer(tab.dta, type="html", title="LC3 Endline", notes = "Two-sided p-values")
# stargazer(tab.dta, type="latex", title="LC3 Endline", notes = "Two-sided p-values") 
#Date and time: Tue, Jan 05, 2021 - 16:26:45

```

## Figure S5: Comparison of incumbent vote share, pre- and post-election results being announced by politician

```{r figS5-pre-post-vote-choice, warning=F, message=F, fig.width=6.5, fig.height=6.5, eval=T}

#format dates
data$lc5.survey.end <- strptime(data$lc5.survey.end, "%Y-%m-%d %H:%M:%S")
data$lc5.survey.start <- strptime(data$lc5.survey.start, "%Y-%m-%d %H:%M:%S")
data$lc5.survey.submission <- strptime(data$lc5.survey.submission, "%Y-%m-%dT%H:%M:%S")

#Equals one if the lc5 survey was completed or submitted prior to 5pm on Feb 25. We check against both submission times and survey end times since some of the early surveys were apparently not correctly closed by RAs at the point of the survey, leading to inconsistent time stamps. 
data$lc5.before.results <- ifelse((data$lc5.survey.end < "2016-02-25 17:00" & data$lc5.survey.end > "2016-02-25 00:00") | (data$lc5.survey.end < "2016-02-25 00:00" & data$lc5.survey.submission < "2016-02-25 17:00"), 1, 0)

#format dates
data$lc3.survey.end <- strptime(data$lc3.survey.end, "%Y-%m-%d %H:%M:%S")
data$lc3.survey.start <- strptime(data$lc3.survey.start, "%Y-%m-%d %H:%M:%S")
data$lc3.survey.submission <- strptime(data$lc3.survey.submission, "%Y-%m-%dT%H:%M:%S")

#Equals one if the lc3 survey was completed or submitted prior to 5pm on March 10
data$lc3.before.results <- ifelse((data$lc3.survey.end < "2016-03-10 17:00" & data$lc3.survey.end > "2016-03-09 00:00"), 1, 0)

#note: removing the original survey submission columns, because RMarkdown replication output will not compile with a dataframe with variables of class `POSIXlt`. These variables are not used elsewhere in the analysis.
data <- data %>%
  select(-c(lc5.survey.end,lc5.survey.start,lc5.survey.submission,
            lc3.survey.end,lc3.survey.start,lc3.survey.submission))

## LC5 plots:

###Creating chairs dataframe and plot
name <- as.character(unique(data$lc5.chair.name))
# table(data$lc5.chair.name,data$lc5.chair.competitive) #To find uncompetitive chairs to remove
chair.not.competitive <- c("bazanye, milton mutabazi","birungi, norman k. b.","kanaku, michael","orot, ismael")
chairs <- data.frame(name[!(name %in% chair.not.competitive)])
names(chairs)[1] <- "name"

for (i in 1:nrow(chairs)){
  sub <- subset(data, lc5.chair.name==chairs$name[i])
  chairs$vote.before[i] <- nrow(sub[sub$lc5.chair.inc.vote==1 & !is.na(sub$lc5.chair.inc.vote) & sub$lc5.before.results==1,])
  chairs$n.before[i] <- nrow(sub[!is.na(sub$lc5.chair.inc.vote) & sub$lc5.before.results==1,])
  chairs$vote.after[i] <- nrow(sub[sub$lc5.chair.inc.vote==1 & !is.na(sub$lc5.chair.inc.vote) & sub$lc5.before.results==0,])
  chairs$n.after[i] <- nrow(sub[!is.na(sub$lc5.chair.inc.vote) & sub$lc5.before.results==0,])
}

chairs <- subset(chairs, n.before>0)
chairs$percent.before <- (chairs$vote.before*100)/chairs$n.before
chairs$percent.after <- (chairs$vote.after*100)/chairs$n.after

chair.ba.lc5 <- ggplot(chairs, aes(x=percent.after, y=percent.before)) +
  geom_point(size=2, shape=20) + theme_bw() + xlab("% Vote for Incumbent (Post-Results)") +
  ylab("% Vote for Incumbent (Pre-Results)") + coord_cartesian(xlim = c(0,100),ylim = c(0,100)) +
  ggtitle("Panel C: LC 5 Chairs") + geom_smooth(method=lm, se=FALSE, color="black")

cor(chairs$percent.before,chairs$percent.after)

##Creating councillor dataframe and plot
tab<-table(data$lc5.councillor.name,data$lc5.councillor.competitive) #To find uncompetitive councillors to remove
tab0<-data.matrix(tab[,1])
tab1<-data.matrix(tab[,2])
tab.dta <- data.frame(tab0,tab1)
tab.dta <- subset(tab.dta, tab0==0 & tab1>0)

councillors <- data.frame(row.names(tab.dta))
names(councillors)[1] <- "name"

for (i in 1:nrow(councillors)){
  sub <- subset(data, lc5.councillor.name==councillors$name[i])
  councillors$vote.before[i] <- nrow(sub[sub$lc5.councillor.inc.vote==1 & !is.na(sub$lc5.councillor.inc.vote) & sub$lc5.before.results==1,])
  councillors$n.before[i] <- nrow(sub[!is.na(sub$lc5.councillor.inc.vote) & sub$lc5.before.results==1,])
  councillors$vote.after[i] <- nrow(sub[sub$lc5.councillor.inc.vote==1 & !is.na(sub$lc5.councillor.inc.vote) & sub$lc5.before.results==0,])
  councillors$n.after[i] <- nrow(sub[!is.na(sub$lc5.councillor.inc.vote) & sub$lc5.before.results==0,])
}

councillors <- subset(councillors, n.before>0 & n.after>0)
councillors$percent.before <- (councillors$vote.before*100)/councillors$n.before
councillors$percent.after <- (councillors$vote.after*100)/councillors$n.after

councillor.ba.lc5 <- ggplot(councillors, aes(x=percent.after, y=percent.before)) +
  geom_point(size=2, shape=20) + theme_bw() + xlab("% Vote for Incumbent (Post-Results)") +
  ylab("% Vote for Incumbent (Pre-Results)") + coord_cartesian(xlim = c(0,100),ylim = c(0,100)) +
  geom_smooth(method=lm, se=FALSE, color="black") + ggtitle("Panel D: LC 5 Councillors")

cor(councillors$percent.before,councillors$percent.after)


## LC3 plots:

###Creating chairs dataframe and plot
name <- as.character(unique(data$lc3.chair.name))
chair.not.competitive=unique(data[data$lc3.chair.competitive==1,]$lc5.chair.name)
chair.not.competitive=chair.not.competitive[!is.na(chair.not.competitive)]
chairs <- data.frame(name[!(name %in% chair.not.competitive)])
names(chairs)[1] <- "name"


for (i in 1:nrow(chairs)){
  sub <- subset(data, lc3.chair.name==chairs$name[i])
  chairs$vote.before[i] <- nrow(sub[sub$lc3.chair.inc.vote==1 & !is.na(sub$lc3.chair.inc.vote) & sub$lc3.before.results==1,])
  chairs$n.before[i] <- nrow(sub[!is.na(sub$lc3.chair.inc.vote) & sub$lc3.before.results==1,])
  chairs$vote.after[i] <- nrow(sub[sub$lc3.chair.inc.vote==1 & !is.na(sub$lc3.chair.inc.vote) & sub$lc3.before.results==0,])
  chairs$n.after[i] <- nrow(sub[!is.na(sub$lc3.chair.inc.vote) & sub$lc3.before.results==0,])
}

chairs <- subset(chairs, n.before>0)
chairs$percent.before <- (chairs$vote.before*100)/chairs$n.before
chairs$percent.after <- (chairs$vote.after*100)/chairs$n.after

chair.ba.lc3 <- ggplot(chairs, aes(x=percent.after, y=percent.before)) +
  geom_point(size=2, shape=20) + theme_bw() + xlab("% Vote for Incumbent (Post-Results)") +
  ylab("% Vote for Incumbent (Pre-Results)") + coord_cartesian(xlim = c(0,100),ylim = c(0,100)) +
  ggtitle("Panel A: LC 3 Chairs") + geom_smooth(method=lm, se=FALSE, color="black")

cor(chairs$percent.before,chairs$percent.after, use="complete") #78

##Creating councillor dataframe and plot
tab<-table(data$lc3.councillor.name,data$lc3.councillor.competitive) #To find uncompetitive councillors to remove
tab0<-data.matrix(tab[,1])
tab1<-data.matrix(tab[,2])
tab.dta <- data.frame(tab0,tab1)
tab.dta <- subset(tab.dta, tab0==0 & tab1>0)

councillors <- data.frame(row.names(tab.dta))
names(councillors)[1] <- "name"

for (i in 1:nrow(councillors)){
  sub <- subset(data, lc3.councillor.name==councillors$name[i])
  councillors$vote.before[i] <- nrow(sub[sub$lc3.councillor.inc.vote==1 & !is.na(sub$lc3.councillor.inc.vote) & sub$lc3.before.results==1,])
  councillors$n.before[i] <- nrow(sub[!is.na(sub$lc3.councillor.inc.vote) & sub$lc3.before.results==1,])
  councillors$vote.after[i] <- nrow(sub[sub$lc3.councillor.inc.vote==1 & !is.na(sub$lc3.councillor.inc.vote) & sub$lc3.before.results==0,])
  councillors$n.after[i] <- nrow(sub[!is.na(sub$lc3.councillor.inc.vote) & sub$lc3.before.results==0,])
}

councillors <- subset(councillors, n.before>0 & n.after>0)
councillors$percent.before <- (councillors$vote.before*100)/councillors$n.before
councillors$percent.after <- (councillors$vote.after*100)/councillors$n.after

councillor.ba.lc3 <- ggplot(councillors, aes(x=percent.after, y=percent.before)) +
  geom_point(size=2, shape=20) + theme_bw() + xlab("% Vote for Incumbent (Post-Results)") +
  ylab("% Vote for Incumbent (Pre-Results)") + coord_cartesian(xlim = c(0,100),ylim = c(0,100)) +
  geom_smooth(method=lm, se=FALSE, color="black") + ggtitle("Panel B: LC 3 Councillors")

cor(councillors$percent.before,councillors$percent.after, use="complete") #64

##Plotting LC3 and LC5 plots together
# pdf("./Figures/before-after_all.pdf", width=6.5, height=6.5)
grid.arrange(chair.ba.lc3, councillor.ba.lc3, chair.ba.lc5, councillor.ba.lc5, ncol = 2, nrow=2)
# dev.off()

```

## Figure S6: Official versus reported votes for incumbents

```{r figS6-official-vs-reported}

## LC3 Councillor:


r2=round(summary(lm(lc3.councillor.percent.ec~lc3.councillor.percent.survey, lc3_councillor_vote_share))$r.squared,3)
r2.lab=paste0("r^2==", r2)
  
plot.councillors.lc3=ggplot(lc3_councillor_vote_share, aes(lc3.councillor.percent.survey, lc3.councillor.percent.ec))+
  geom_point(alpha = 20/20, position=position_jitter(width=0,height=0))+
  scale_colour_manual(name="",  values =c("black"))+
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE, colour="black")+
  scale_fill_brewer(guide = guide_legend(title = ""))+
  labs(x = "% Votes for Incumbent (Survey)",
       y=  "% Votes for Incumbent (Official)"
  )+
  ylim(0,100)+
  theme_bw()+
  ggtitle("Panel B: LC 3 Councillors")+  annotate("text", x=15, y=90, label= r2.lab, parse=TRUE) 

##LC3 Chair:

r2=round(summary(lm(lc3.chair.percent.ec~lc3.chair.percent.survey, lc3_chair_vote_share))$r.squared,3)
r2.lab=paste0("r^2==", r2)

plot.chairs.lc3=ggplot(lc3_chair_vote_share, aes(lc3.chair.percent.survey, lc3.chair.percent.ec))+
  geom_point(alpha = 20/20, position=position_jitter(width=0,height=0))+
  scale_colour_manual(name="",  values =c("black"))+
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE, colour="black")+
  scale_fill_brewer(guide = guide_legend(title = ""))+
  labs(x = "% Votes for Incumbent (Survey)",
       y=  "% Votes for Incumbent (Official)"
  )+
  ylim(0,100)+
  theme_bw()+
  ggtitle("Panel A: LC 3 Chairs")+  annotate("text", x=15, y=90, label= r2.lab, parse=TRUE) 


## LC5 councillor:

#exclude any cases with only one survey vote for the incumbent (since does not add meaningful data to the plot)
lc5_councillor_vote_share=lc5_councillor_vote_share[lc5_councillor_vote_share$number_votes>1,]

r2=round(summary(lm(incumbent_share_official~incumbent_share_survey, lc5_councillor_vote_share))$r.squared,3)
r2.lab=paste0("r^2==", r2)


plot.councillors.lc5=ggplot(lc5_councillor_vote_share, aes(incumbent_share_survey, incumbent_share_official))+
  geom_point(alpha = 20/20, position=position_jitter(width=0,height=0))+
  scale_colour_manual(name="",  values =c("black"))+
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE, colour="black")+
  scale_fill_brewer(guide = guide_legend(title = ""))+
  labs(x = "% Votes for Incumbent (Survey)",
       y=  "% Votes for Incumbent (Official)"
  )+
  ylim(0,100)+
  theme_bw()+
  ggtitle("Panel D: LC 5 Councillors")+  annotate("text", x=15, y=90, label= r2.lab, parse=TRUE) 


##LC5 chair

#exclude any cases with only one survey vote for the incumbent (since does not add meaningful data to the plot). 
lc5_chair_vote_share=lc5_chair_vote_share[lc5_chair_vote_share$number_votes>1,]

r2=round(summary(lm(incumbent_share_official~incumbent_share_survey, lc5_chair_vote_share))$r.squared,3)
r2.lab=paste0("r^2==", r2)

plot.chairs.lc5=ggplot(lc5_chair_vote_share, aes(incumbent_share_survey, incumbent_share_official))+
  geom_point(alpha = 20/20, position=position_jitter(width=0,height=0))+
  scale_colour_manual(name="",  values =c("black"))+
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE, colour="black")+
  scale_fill_brewer(guide = guide_legend(title = ""))+
  labs(x = "% Votes for Incumbent (Survey)",
       y=  "% Votes for Incumbent (Official)"
  )+
  ylim(0,100)+
  theme_bw()+
  ggtitle("Panel C: LC 5 Chairs")+  annotate("text", x=15, y=90, label= r2.lab, parse=TRUE) 



# Plotting together:
#pdf("./Figures/official_vs_reported_all.pdf",width=6.5,height=6.5)
grid.arrange(plot.chairs.lc3, plot.councillors.lc3, plot.chairs.lc5, plot.councillors.lc5, nrow = 2, ncol=2)
# dev.off()




```


## Figure S7: Reported Polling Station Basin Color for LC3 and LC5 Elections

```{r figS7-basin-color, fig.width=20, fig.height=20, warning=F}

##LC3:

#Function for returning the mode of a distribution as a single integer
SingleMode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

#Create a data frame that excludes cases where basin color is non responsive or missing
data.temp=data[data$sc.color_of_water_basin !="don_t_remember",]
data.temp=data.temp[data.temp$sc.color_of_water_basin !="refused_to_ans",]
data.temp=data.temp[data.temp$sc.color_of_water_basin !="another_object",]
data.temp=data.temp[data.temp$sc.color_of_water_basin !="",]
data.temp$basin_color=factor(data.temp$sc.color_of_water_basin)
data.temp$basin_color_int = as.integer(data.temp$basin_color)

#create a simple data frame for plotting
data.simple=data.frame(badnews=data.temp$ps.bad.noNA, goodnews=data.temp$ps.good.noNA, treat=data.temp$ps.treat, village=data.temp$location.id, district=data.temp$district_lower, basin_color_int=data.temp$basin_color_int, count=rep_len(1,nrow(data.temp)))

#check whether respondent color choices equal the modal choice in a village
data.aggregate=aggregate(data.simple, by=list(village_name=data.simple$village), FUN=SingleMode)
data.merge=merge(data.simple, data.aggregate, by="village")
data.merge$count=1

#set correct=TRUE if respondent choice matches modal choice
data.merge$correct = NA
data.merge$correct = ifelse(data.merge$basin_color_int.x==data.merge$basin_color_int.y, TRUE, data.merge$correct)
data.merge$correct = ifelse(data.merge$basin_color_int.x!=data.merge$basin_color_int.y, FALSE, data.merge$correct)


#Calculdate mean correct in treatment group
data.treat=data.merge[data.merge$treat.x==1,]
data.treat=data.treat[!is.na(data.treat$district.x),]
m.treat=round(mean(data.treat$correct),3)
m.treat.bad=round(mean(data.treat[data.treat$badnews.x==1,]$correct, na.rm = TRUE),3)
m.treat.good=round(mean(data.treat[data.treat$goodnews.x==1,]$correct, na.rm = TRUE),3)

#Calculdate mean correct in control group
data.control=data.merge[data.merge$treat.x==0,]
data.control=data.control[!is.na(data.control$district.x),]
m.control=round(mean(data.control$correct),3)

m.control.bad=round(mean(data.control[data.control$badnews.x==1,]$correct, na.rm = TRUE),3)
m.control.good=round(mean(data.control[data.control$goodnews.x==1,]$correct, na.rm = TRUE),3)


treatplot.lc3=ggplot(data.treat, aes(reorder(factor(data.treat$district.x), data.treat$count, sum), fill=factor(correct))) +
  geom_bar()+
  theme_bw()+
  scale_fill_manual(guide = guide_legend(title = "Same as Modal \nResponse",
                                         keywidth=3,
                                         keyheight=3),
                    values=c("#d0d3d4","#000000"),
                    labels = c("False", "True", "")
  )+
  theme(axis.text = element_text(size = 24, angle = 90,hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=25),
        legend.text=element_text(size = 22),
        legend.title=element_text(size = 22),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        plot.title = element_text(size=22)
        
  )+
  ylab("Number of Respondents")+
  scale_y_continuous(expand=c(0.02,0), limits = c(0, 500))+
  annotate(geom="text", x=12, y=450, label="Treatment Group", size=15,
           color="black")+
  annotate(geom="text", x=12, y=410, label=paste("mu==",m.treat), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=380, label=paste("mu['+']==",m.treat.good), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=350, label=paste("mu['-']==",m.treat.bad), size=12,
           color="black", parse=TRUE)+ggtitle("Panel A: LC3 Election")



controlplot.lc3=ggplot(data.control, aes(reorder(factor(data.control$district.x), data.control$count, sum), fill=factor(correct))) +
  geom_bar()+
  theme_bw()+
  scale_fill_manual(guide = guide_legend(title = "Same as Modal \nResponse",
                                         keywidth=3,
                                         keyheight=3),
                    values=c("#d0d3d4","#000000"),
                    labels = c("False", "True", "")
  )+
  theme(axis.text = element_text(size = 24, angle = 90,hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=25),
        legend.text=element_text(size = 22),
        legend.title=element_text(size = 22),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size=22)
        
  )+
  ylab("Number of Respondents")+
  scale_y_continuous(expand=c(0.02,0), limits = c(0, 500))+
  annotate(geom="text", x=12, y=450, label="Control Group", size=15,
           color="black")+
  annotate(geom="text", x=12, y=410, label=paste("mu==",m.control), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=380, label=paste("mu['+']==",m.control.good), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=350, label=paste("mu['-']==",m.control.bad), size=12,
           color="black", parse=TRUE)+ggtitle(" ")

data.simple=NULL
data.temp=NULL
data.merge=NULL


##LC5:

#Create a data frame that excludes cases where basin color is non responsive or missing
data.temp=data[data$d.color_of_water_basin.x !="don_t_remember",]
data.temp=data.temp[data.temp$d.color_of_water_basin.x !="refused_to_ans",]
data.temp=data.temp[data.temp$d.color_of_water_basin.x !="another_object",]
data.temp=data.temp[data.temp$d.color_of_water_basin.x !="",]
data.temp$basin_color=factor(data.temp$d.color_of_water_basin.x)
data.temp$basin_color_int = as.integer(data.temp$basin_color)

#create an indicator variable for whether respondent is in good or bad news group
data.temp$goodnews = ifelse(data.temp$budget.actual>data.temp$budget.prior | (data.temp$budget.actual==data.temp$budget.prior & data.temp$budget.actual>=4), 1, 0)
data.temp$badnews = ifelse(data.temp$budget.actual<data.temp$budget.prior | (data.temp$budget.actual==data.temp$budget.prior & data.temp$budget.actual<=2), 1, 0)

#create a simple data frame for plotting
data.simple=data.frame(badnews=data.temp$badnews, goodnews=data.temp$goodnews, treat=data.temp$budget.treat, village=data.temp$location.id, district=data.temp$district_lower, basin_color_int=data.temp$basin_color_int, count=rep_len(1,nrow(data.temp)))

#check whether respondent color choices equal the modal choice in a village
data.aggregate=aggregate(data.simple, by=list(village_name=data.simple$village), FUN=SingleMode)
data.merge=merge(data.simple, data.aggregate, by="village")
data.merge$count=1
#set correct=TRUE if respondent choice matches modal choice
data.merge$correct = NA
data.merge$correct = ifelse(data.merge$basin_color_int.x==data.merge$basin_color_int.y, TRUE, data.merge$correct)
data.merge$correct = ifelse(data.merge$basin_color_int.x!=data.merge$basin_color_int.y, FALSE, data.merge$correct)

#calculate mean correct in the treatment group
data.treat1=data.merge[data.merge$treat.x==1,]
data.treat1=data.treat1[!is.na(data.treat1$district.x),]
m.treat=round(mean(data.treat1$correct),3)
m.treat.bad=round(mean(data.treat1[data.treat1$badnews.x==1,]$correct, na.rm = TRUE),3)
m.treat.good=round(mean(data.treat1[data.treat1$goodnews.x==1,]$correct, na.rm = TRUE),3)

#calculate mean correct in the control group
data.control1=data.merge[data.merge$treat.x==0,]
data.control1=data.control1[!is.na(data.control1$district.x),]
m.control=round(mean(data.control1$correct),3)
m.control.bad=round(mean(data.control1[data.control1$badnews.x==1,]$correct, na.rm = TRUE),3)
m.control.good=round(mean(data.control1[data.control1$goodnews.x==1,]$correct, na.rm = TRUE),3)

treatplot.lc5=ggplot(data.treat1, aes(reorder(factor(data.treat1$district.x), data.treat1$count, sum), fill=factor(correct))) +
  geom_bar()+
  theme_bw()+
  scale_fill_manual(guide = guide_legend(title = "Same as Modal \nResponse",
                                         keywidth=3,
                                         keyheight=3),
                    values=c("#d0d3d4","#000000"),
                    labels = c("False", "True", "")
  )+
  theme(axis.text = element_text(size = 24, angle = 90,hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=25),
        legend.text=element_text(size = 22),
        legend.title=element_text(size = 22),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        plot.title = element_text(size=22)
        
  )+
  ylab("Number of Respondents")+
  scale_y_continuous(expand=c(0.02,0), limits = c(0, 500))+
  annotate(geom="text", x=12, y=450, label="Treatment Group", size=15,
           color="black")+
  annotate(geom="text", x=12, y=410, label=paste("mu==",m.treat), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=380, label=paste("mu['+']==",m.treat.good), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=350, label=paste("mu['-']==",m.treat.bad), size=12,
           color="black", parse=TRUE)+ggtitle("Panel B: LC5 Election")



controlplot.lc5=ggplot(data.control1, aes(reorder(factor(data.control1$district.x), data.control1$count, sum), fill=factor(correct))) +
  geom_bar()+
  theme_bw()+
  scale_fill_manual(guide = guide_legend(title = "Same as Modal \nResponse",
                                         keywidth=3,
                                         keyheight=3),
                    values=c("#d0d3d4","#000000"),
                    labels = c("False", "True", "")
  )+
  theme(axis.text = element_text(size = 24, angle = 90,hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=25),
        legend.text=element_text(size = 22),
        legend.title=element_text(size = 22),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size=22)
        
  )+
  ylab("Number of Respondents")+
  scale_y_continuous(expand=c(0.02,0), limits = c(0, 500))+
  annotate(geom="text", x=12, y=450, label="Control Group", size=15,
           color="black")+
  annotate(geom="text", x=12, y=410, label=paste("mu==",m.control), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=380, label=paste("mu['+']==",m.control.good), size=12,
           color="black", parse=TRUE)+
  annotate(geom="text", x=12, y=350, label=paste("mu['-']==",m.control.bad), size=12,
           color="black", parse=TRUE)+ggtitle(" ")

# pdf("./Figures/basinplot.pdf",width=20,height=20)
grid.arrange(treatplot.lc3, controlplot.lc3, treatplot.lc5, controlplot.lc5, ncol = 2, nrow=2, widths=c(8.7,11.3))

# dev.off()
# knitr::include_graphics("./Figures/basinplot.pdf")

data.simple=NULL
data.temp=NULL
data.merge=NULL
data.treat=NULL
data.control1=NULL

```

## Figure S8: Treatment effects for main vote choice and turnout outcomes estimated as pre-specified

```{r figS8-direct-prespecified, fig.height=7, fig.width = 8, eval=T}

analysis_chunks_pap_spec <- read.csv("./analysis_chunks_pap_spec.csv")

felm_chunk <- function(chunk_dta){
  my_list <- list()
  for (i in 1:nrow(chunk_dta)){
    my_list[[i]] <- felm(formula = as.formula(chunk_dta$formula[i]),
                      data = eval(parse(text=chunk_dta$data[i])))
  }
  names(my_list) <- chunk_dta$label
  return(my_list)
}


chunk_out <- felm_chunk(analysis_chunks_pap_spec) #this object contains all the felm() objects

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

results_out <- tibble(names(chunk_out)) %>% rename(label="names(chunk_out)") %>%
  mutate(hypothesis=substr(label,1,3)) %>%
  mutate(outcome_var=str_extract(analysis_chunks_pap_spec$formula, "[^ ~]+")) %>%
  mutate(outcome_var=factor(outcome_var,
                               levels=c("lc5.chair.transformed",
                                        "lc5.councillor.transformed",
                                        "lc3.chair.transformed",
                                        "lc3.councillor.transformed",
                                        "lc5.turnout.transformed",
                                        "lc3.turnout.transformed"))) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(estimate=sapply(chunk_out, coef_grab)) %>%
  mutate(se=sapply(chunk_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h1a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h1a")

h1a <- ggplot(data = results_out_h1a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h1a$outcome_var),
                     labels = c("LC5 Chair","LC5 Councillor", 
                                "LC3 Chair", "LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.08,0.11) +
  ggtitle("Vote for Incumbent, Good News")

results_out_h1b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h1b")

h1b <- ggplot(data = results_out_h1b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h1b$outcome_var),
                     labels = c("LC5 Chair","LC5 Councillor", 
                                "LC3 Chair", "LC3 Councillor")) +
    labs(x = "Office", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.08,0.11) +
  ggtitle("Vote for Incumbent, Bad News")


results_out_h7a <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h7a")

h7a <- ggplot(data = results_out_h7a,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h7a$outcome_var),
                     labels = c("LC5","LC3")) +
    labs(x = "Election Type", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.03,0.06) +
  ggtitle("Turnout, Good News")


results_out_h7b <- results_out %>% 
  filter(substr(hypothesis,1,3)=="h7b")

h7b <- ggplot(data = results_out_h7b,
            mapping = aes(x = outcome_var, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h7b$outcome_var),
                     labels = c("LC5","LC3")) +
    labs(x = "Election Type", 
         y = "Treatment Effect") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = outcome_var,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.03,0.06) +
  ggtitle("Turnout, Bad News")

# pdf(file="combined-direct-pap-210331.pdf", height=7, width = 8)
ggarrange(h1a,h1b,h7a,h7b,
          ncol = 2, nrow = 2,
          labels = c("A","B","C","D"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```

## Figure S9: Conditional effects of treatment with good and bad news subgroups for LC5 Chairperson elections

```{r lc5-chair-cate-analysis, eval=T}

##H2: Difference with priors

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
     mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                               office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                               office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                               office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

#keeping only LC5 Chair
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)

#Subsetting execution:
keep <- ifelse(dta$finalps.prior!=round(dta$finalps.prior), 0, keep) #removing imputed values of the moderator

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta$finalps.prior.diff <- as.factor(dta$finalps.prior.diff)
l <- levels(dta$finalps.prior.diff) #-4 to 4

#without covariates:
for (i in 5:9){ #levels relevant to good news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h2a_",l[i]), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", "")),
              h2b.ps.pooled)
}

#with covariates:
for (i in 5:9){ #levels relevant to good news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h2a_",l[i],"_cov"), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))

h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", ""),"_cov"),
              h2b.ps.pooled)
}

#collecting both with/without cov output into list:
h2_out_lc5_chair <- list(h2a_0,
               h2a_1,
               h2a_2,
               h2a_3,
               h2a_4,
               h2b_0,
               h2b_1,
               h2b_2,
               h2b_3,
               h2b_4,
               h2a_0_cov,
               h2a_1_cov,
               h2a_2_cov,
               h2a_3_cov,
               h2a_4_cov,
               h2b_0_cov,
               h2b_1_cov,
               h2b_2_cov,
               h2b_3_cov,
               h2b_4_cov
               )

h2_out_labels <- c("h2a_0",
                   "h2a_1",
                   "h2a_2",
                   "h2a_3",
                   "h2a_4",
                   "h2b_0",
                   "h2b_1",
                   "h2b_2",
                   "h2b_3",
                   "h2b_4",
                   "h2a_0_cov",
                   "h2a_1_cov",
                   "h2a_2_cov",
                   "h2a_3_cov",
                   "h2a_4_cov",
                   "h2b_0_cov",
                   "h2b_1_cov",
                   "h2b_2_cov",
                   "h2b_3_cov",
                   "h2b_4_cov"
               )

names(h2_out_lc5_chair) <- h2_out_labels

##H5: Uncertainty

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(finalps.certainty = factor(finalps.certainty,
                                    levels=rev(c("very_uncertain","not_certain",
                                                 "certain","very_certain"))))

#subsetting on pooled data
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$finalps.certainty) #very_certain to very_uncertain

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))

h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h5a_",i), h5a.ps.pooled)

h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h5b_",i), h5b.ps.pooled)
}


#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))

h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h5a_",i,"_cov"), h5a.ps.pooled)

h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h5b_",i,"_cov"), h5b.ps.pooled)
}

h5_out_lc5_chair <- list(h5a_1,
               h5a_2,
               h5a_3,
               h5a_4,
               h5b_1,
               h5b_2,
               h5b_3,
               h5b_4,
               h5a_1_cov,
               h5a_2_cov,
               h5a_3_cov,
               h5a_4_cov,
               h5b_1_cov,
               h5b_2_cov,
               h5b_3_cov,
               h5b_4_cov
            )

h5_out_labels <- c("h5a_1",
                   "h5a_2",
                   "h5a_3",
                   "h5a_4",
                   "h5b_1",
                   "h5b_2",
                   "h5b_3",
                   "h5b_4",
                   "h5a_1_cov",
                   "h5a_2_cov",
                   "h5a_3_cov",
                   "h5a_4_cov",
                   "h5b_1_cov",
                   "h5b_2_cov",
                   "h5b_3_cov",
                   "h5b_4_cov"
            )

names(h5_out_lc5_chair) <- h5_out_labels

##H6: Importance

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(ps.importance = factor(ps.importance, levels=c("very_important","somewhat_impor","not_very_impor","not_important")))

#subsetting on LC5 Chair
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- rev(levels(dta$ps.importance)) #not_important to very_important

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))

h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h6a_",i), h6a.ps.pooled)

h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h6b_",i), h6b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))

h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h6a_",i,"_cov"), h6a.ps.pooled)

h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h6b_",i,"_cov"), h6b.ps.pooled)
}

#collecting model outputs:
h6_out_lc5_chair <- list(h6a_1,
               h6a_2,
               h6a_3,
               h6a_4,
               h6b_1,
               h6b_2,
               h6b_3,
               h6b_4,
               h6a_1_cov,
               h6a_2_cov,
               h6a_3_cov,
               h6a_4_cov,
               h6b_1_cov,
               h6b_2_cov,
               h6b_3_cov,
               h6b_4_cov
            )

h6_out_labels <- c("h6a_1",
                   "h6a_2",
                   "h6a_3",
                   "h6a_4",
                   "h6b_1",
                   "h6b_2",
                   "h6b_3",
                   "h6b_4",
                   "h6a_1_cov",
                   "h6a_2_cov",
                   "h6a_3_cov",
                   "h6a_4_cov",
                   "h6b_1_cov",
                   "h6b_2_cov",
                   "h6b_3_cov",
                   "h6b_4_cov"
            )

names(h6_out_lc5_chair) <- h6_out_labels

##H8: Alignment and turnout

#pooling data
dta <- data %>% gather(e_level,turnout,c("lc5.turnout","lc3.turnout")) %>%
                mutate(turnout.intent = 
                         case_when(e_level=="lc5.turnout" ~ lc5.turnout.intent,
                                   e_level=="lc3.turnout" ~ lc3.turnout.intent)) %>%
  mutate(aligned.inc = case_when(
    e_level=="lc5.turnout" ~ aligned.lc5.chair.inc,
    e_level=="lc3.turnout" ~ aligned.lc3.chair.inc)) %>%
  mutate(aligned.inc = factor(aligned.inc, labels = c("not_aligned", "aligned"))) 

#subsetting on LC5 election
keep <- ifelse(dta$e_level=="lc5.turnout", 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$aligned.inc) #not_aligned & aligned

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))

h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
assign(paste0("h8a_",i), h8a.ps.pooled)

h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
assign(paste0("h8b_",i), h8b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))

h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
assign(paste0("h8a_",i,"_cov"), h8a.ps.pooled)

h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
assign(paste0("h8b_",i,"_cov"), h8b.ps.pooled)
}

#collecting model outputs:
h8_out_lc5 <- list(h8a_1,
               h8a_2,
               h8b_1,
               h8b_2,
               h8a_1_cov,
               h8a_2_cov,
               h8b_1_cov,
               h8b_2_cov
            )

h8_out_labels <- c("h8a_1",
                   "h8a_2",
                   "h8b_1",
                   "h8b_2",
                   "h8a_1_cov",
                   "h8a_2_cov",
                   "h8b_1_cov",
                   "h8b_2_cov"
            )

names(h8_out_lc5) <- h8_out_labels

##H10: Vote buying

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
                mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                                          office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                                          office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                                          office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(received.gifts = 
           case_when(office=="lc5.chair.inc.vote" ~ lc5.received.gifts,
                     office=="lc5.councillor.inc.vote" ~ lc5.received.gifts,
                     office=="lc3.chair.inc.vote" ~ lc3.received.gifts,
                     office=="lc3.councillor.inc.vote" ~ lc3.received.gifts))

#subsetting on LC5 Chair data
keep <- ifelse(dta$office=="lc5.chair.inc.vote" & dta$lc5.chair.competitive==1 & dta$lc5.chair.party.switch==0, 1, 0)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(received.gifts = factor(received.gifts,  
                labels = c("no gifts", "received gifts")))

#pooled regression, looping through base levels
l <- rev(levels(dta$received.gifts)) #received gifts & no gifts

#without covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))

h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h10a_",i), h10a.ps.pooled)

h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h10b_",i), h10b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))

h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.good.noNA==1))
assign(paste0("h10a_",i,"_cov"), h10a.ps.pooled)

h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                 data=filter(dta, ps.bad.noNA==1))
assign(paste0("h10b_",i,"_cov"), h10b.ps.pooled)
}

#collecting model outputs:
h10_out_lc5_chair <- list(h10a_1,
                h10a_2,
                h10b_1,
                h10b_2,
                h10a_1_cov,
                h10a_2_cov,
                h10b_1_cov,
                h10b_2_cov
            )

h10_out_labels <- c("h10a_1",
                    "h10a_2",
                    "h10b_1",
                    "h10b_2",
                    "h10a_1_cov",
                    "h10a_2_cov",
                    "h10b_1_cov",
                    "h10b_2_cov"
            )

names(h10_out_lc5_chair) <- h10_out_labels

```

```{r figS9-cate-lc5chair, fig.height=10, fig.width = 10}

hte_out <- c(h2_out_lc5_chair,
             h5_out_lc5_chair,
             h6_out_lc5_chair,
             h8_out_lc5,
             h10_out_lc5_chair) 

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

#outcome variable:
#h2=inc.vote
#h5=inc.vote
#h6=inc.vote
#h8=turnout
#h9=inc.vote
#h10=inc.vote

results_out <- tibble(names(hte_out)) %>% rename(label="names(hte_out)") %>%
  mutate(hypothesis=word(label,1,sep = "\\_")) %>%
  mutate(outcome_var=ifelse(hypothesis %in% c("h8a","h8b"),"turnout","inc.vote")) %>%
  mutate(mod_label = case_when(label=="h2a_4" ~ "+4", 
                               label=="h2a_3" ~ "+3",
                               label=="h2a_2" ~ "+2", 
                               label=="h2a_1" ~ "+1",
                               label=="h2a_0" ~ "same",
                               label=="h2b_0" ~ "same", 
                               label=="h2b_1" ~ "-1",
                               label=="h2b_2" ~ "-2", 
                               label=="h2b_3" ~ "-3",
                               label=="h2b_4" ~ "-4",
                               label=="h5a_4" ~ "Very\nUncertain", 
                               label=="h5a_3" ~ "Not\nCertain",
                               label=="h5a_2" ~ "Certain", 
                               label=="h5a_1" ~ "Very\nCertain",
                               label=="h5b_4" ~ "Very\nUncertain", 
                               label=="h5b_3" ~ "Not\nCertain",
                               label=="h5b_2" ~ "Certain", 
                               label=="h5b_1" ~ "Very\nCertain",
                               label=="h6a_1" ~ "Not\nImportant", 
                               label=="h6a_2" ~ "Not Very\nImportant",
                               label=="h6a_3" ~ "Somewhat\nImportant", 
                               label=="h6a_4" ~ "Very\nImportant",
                               label=="h6b_1" ~ "Not\nImportant", 
                               label=="h6b_2" ~ "Not Very\nImportant",
                               label=="h6b_3" ~ "Somewhat\nImportant", 
                               label=="h6b_4" ~ "Very\nImportant",
                               label=="h8a_2" ~ "Aligned", 
                               label=="h8a_1" ~ "Not Aligned",
                               label=="h8b_2" ~ "Aligned", 
                               label=="h8b_1" ~ "Not Aligned",
                               label=="h10a_1" ~ "Received Gifts", 
                               label=="h10a_2" ~ "No Gifts",
                               label=="h10b_1" ~ "Received Gifts", 
                               label=="h10b_2" ~ "No Gifts",
                               label=="h2a_4_cov" ~ "+4", 
                               label=="h2a_3_cov" ~ "+3",
                               label=="h2a_2_cov" ~ "+2", 
                               label=="h2a_1_cov" ~ "+1",
                               label=="h2a_0_cov" ~ "same",
                               label=="h2b_0_cov" ~ "same", 
                               label=="h2b_1_cov" ~ "-1",
                               label=="h2b_2_cov" ~ "-2", 
                               label=="h2b_3_cov" ~ "-3",
                               label=="h2b_4_cov" ~ "-4",
                               label=="h5a_4_cov" ~ "Very\nUncertain", 
                               label=="h5a_3_cov" ~ "Not\nCertain",
                               label=="h5a_2_cov" ~ "Certain", 
                               label=="h5a_1_cov" ~ "Very\nCertain",
                               label=="h5b_4_cov" ~ "Very\nUncertain", 
                               label=="h5b_3_cov" ~ "Not\nCertain",
                               label=="h5b_2_cov" ~ "Certain", 
                               label=="h5b_1_cov" ~ "Very\nCertain",
                               label=="h6a_1_cov" ~ "Not\nImportant", 
                               label=="h6a_2_cov" ~ "Not Very\nImportant",
                               label=="h6a_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6a_4_cov" ~ "Very\nImportant",
                               label=="h6b_1_cov" ~ "Not\nImportant", 
                               label=="h6b_2_cov" ~ "Not Very\nImportant",
                               label=="h6b_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6b_4_cov" ~ "Very\nImportant",
                               label=="h8a_2_cov" ~ "Aligned", 
                               label=="h8a_1_cov" ~ "Not Aligned",
                               label=="h8b_2_cov" ~ "Aligned", 
                               label=="h8b_1_cov" ~ "Not Aligned",
                               label=="h9a_1_cov" ~ "same tribe", 
                               label=="h9a_2_cov" ~ "not same tribe",
                               label=="h9b_1_cov" ~ "same tribe", 
                               label=="h9b_2_cov" ~ "not same tribe",
                               label=="h10a_1_cov" ~ "Received Gifts", 
                               label=="h10a_2_cov" ~ "No Gifts",
                               label=="h10b_1_cov" ~ "Received Gifts", 
                               label=="h10b_2_cov" ~ "No Gifts"
                               )) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(hte_out, coef_grab)) %>%
  mutate(se=sapply(hte_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h2a <- results_out %>%
  filter(hypothesis=="h2a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","+1","+2","+3","+4")))

h2a <- ggplot(data = results_out_h2a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h2a$mod_label),
                     labels = unique(results_out_h2a$mod_label)) +
    labs(x = "(Actual News - Prior Belief)", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.45,0.25) +
  ggtitle("Difference w/ Priors, Good News")


results_out_h2b <- results_out %>%
  filter(hypothesis=="h2b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","-1","-2","-3","-4")))

h2b <- ggplot(data = results_out_h2b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h2b$mod_label),
                     labels = unique(results_out_h2b$mod_label)) +
    labs(x = "(Actual News - Prior Belief)", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.45,0.25) +
  ggtitle("Difference w/ Priors, Bad News")


results_out_h5a <- results_out %>%
  filter(hypothesis=="h5a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5a <- ggplot(data = results_out_h5a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h5a$mod_label),
                     labels = unique(results_out_h5a$mod_label)) +
    labs(x = "Level of Certainty about Prior", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.2) +
  ggtitle("Certainty, Good News")

results_out_h5b <- results_out %>%
  filter(hypothesis=="h5b") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5b <- ggplot(data = results_out_h5b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h5b$mod_label),
                     labels = unique(results_out_h5b$mod_label)) +
    labs(x = "Level of Certainty about Prior", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.2) +
  ggtitle("Certainty, Bad News")


results_out_h6a <- results_out %>%
  filter(hypothesis=="h6a") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6a <- ggplot(data = results_out_h6a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h6a$mod_label),
                     labels = unique(results_out_h6a$mod_label)) +
    labs(x = "Importance of Public Services", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.3,0.55) +
  ggtitle("Importance, Good News")

results_out_h6b <- results_out %>%
  filter(hypothesis=="h6b") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6b <- ggplot(data = results_out_h6b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h6b$mod_label),
                     labels = unique(results_out_h6b$mod_label)) +
    labs(x = "Importance of Public Services", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.3,0.55) +
  ggtitle("Importance, Bad News")


results_out_h8a <- results_out %>%
  filter(hypothesis=="h8a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Not Aligned","Aligned")))

h8a <- ggplot(data = results_out_h8a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h8a$mod_label),
                     labels = unique(results_out_h8a$mod_label)) +
    labs(x = "Alignment w/ Incumbent Chair", 
         y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.05,0.085) +
  ggtitle("Alignment, Good News")

results_out_h8b <- results_out %>%
  filter(hypothesis=="h8b") # %>%
  #mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h8b <- ggplot(data = results_out_h8b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h8b$mod_label),
                     labels = unique(results_out_h8b$mod_label)) +
    labs(x = "Alignment w/ Incumbent Chair", 
         y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.05,0.085) +
  ggtitle("Alignment, Bad News")


results_out_h10a <- results_out %>%
  filter(hypothesis=="h10a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10a <- ggplot(data = results_out_h10a,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h10a$mod_label),
                     labels = unique(results_out_h10a$mod_label)) +
    labs(x = "Receipt of Gifts Prior to Election", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.18,0.1) +
  ggtitle("Vote Buying, Good News")

results_out_h10b <- results_out %>%
  filter(hypothesis=="h10b")  %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10b <- ggplot(data = results_out_h10b,
            mapping = aes(x = mod_label, y = estimate,
                          ymin = ci_95_lower,
                          ymax = ci_95_upper,
                          group = covariates, color = covariates)) +
    geom_pointrange(position = position_dodge(width = 0.5)) +
    scale_x_discrete(breaks = unique(results_out_h10b$mod_label),
                     labels = unique(results_out_h10b$mod_label)) +
    labs(x = "Receipt of Gifts Prior to Election", 
         y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                          ymin = ci_90_lower,
                          ymax = ci_90_upper,
                          group = covariates, color = covariates),
                size=1.5, width=0) + 
                theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.18,0.1) +
  ggtitle("Vote Buying, Bad News")

# pdf(file="lc5-chair-hte-cov-200623.pdf", height=10, width = 10)
ggarrange(h2a,h2b,h5a,h5b,h6a,h6b,h8a,h8b,h10a,h10b,
          ncol = 2, nrow = 5,
          labels = c("A","B","C","D","E","F","G","H","K","L"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```


## Figure S10: Conditional effects of treatment with good and bad news subgroups for LC5 Councillor elections

```{r lc5-councillor-cate-analysis}

##H2: Difference in prior beliefs

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

#keeping only LC5 Councillor
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0, 1, 0)

#Subsetting execution:
keep <- ifelse(dta$finalps.prior!=round(dta$finalps.prior), 0, keep) #removing imputed values of the moderator

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta$finalps.prior.diff <- as.factor(dta$finalps.prior.diff)
l <- levels(dta$finalps.prior.diff) #-4 to 4

#without covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i]), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", "")),
         h2b.ps.pooled)
}

#with covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i],"_cov"), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", ""),"_cov"),
         h2b.ps.pooled)
}

#collecting both with/without cov output into list:
h2_out_lc5_councillor <- list(h2a_0,
                              h2a_1,
                              h2a_2,
                              h2a_3,
                              h2a_4,
                              h2b_0,
                              h2b_1,
                              h2b_2,
                              h2b_3,
                              h2b_4,
                              h2a_0_cov,
                              h2a_1_cov,
                              h2a_2_cov,
                              h2a_3_cov,
                              h2a_4_cov,
                              h2b_0_cov,
                              h2b_1_cov,
                              h2b_2_cov,
                              h2b_3_cov,
                              h2b_4_cov
)

h2_out_labels <- c("h2a_0",
                   "h2a_1",
                   "h2a_2",
                   "h2a_3",
                   "h2a_4",
                   "h2b_0",
                   "h2b_1",
                   "h2b_2",
                   "h2b_3",
                   "h2b_4",
                   "h2a_0_cov",
                   "h2a_1_cov",
                   "h2a_2_cov",
                   "h2a_3_cov",
                   "h2a_4_cov",
                   "h2b_0_cov",
                   "h2b_1_cov",
                   "h2b_2_cov",
                   "h2b_3_cov",
                   "h2b_4_cov"
)

names(h2_out_lc5_councillor) <- h2_out_labels


##H5: Uncertainty

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(finalps.certainty = factor(finalps.certainty,
                                    levels=rev(c("very_uncertain","not_certain",
                                                 "certain","very_certain"))))

#subsetting on pooled data
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$finalps.certainty) #very_uncertain to certain

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i), h5b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i,"_cov"), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i,"_cov"), h5b.ps.pooled)
}

h5_out_lc5_councillor <- list(h5a_1,
                              h5a_2,
                              h5a_3,
                              h5a_4,
                              h5b_1,
                              h5b_2,
                              h5b_3,
                              h5b_4,
                              h5a_1_cov,
                              h5a_2_cov,
                              h5a_3_cov,
                              h5a_4_cov,
                              h5b_1_cov,
                              h5b_2_cov,
                              h5b_3_cov,
                              h5b_4_cov
)

h5_out_labels <- c("h5a_1",
                   "h5a_2",
                   "h5a_3",
                   "h5a_4",
                   "h5b_1",
                   "h5b_2",
                   "h5b_3",
                   "h5b_4",
                   "h5a_1_cov",
                   "h5a_2_cov",
                   "h5a_3_cov",
                   "h5a_4_cov",
                   "h5b_1_cov",
                   "h5b_2_cov",
                   "h5b_3_cov",
                   "h5b_4_cov"
)

names(h5_out_lc5_councillor) <- h5_out_labels


##H6: Importance

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(ps.importance = factor(ps.importance, levels=c("very_important","somewhat_impor","not_very_impor","not_important")))

#subsetting on LC5 Chair
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- rev(levels(dta$ps.importance)) #not_important to very_important

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i), h6b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i,"_cov"), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i,"_cov"), h6b.ps.pooled)
}

#collecting model outputs:
h6_out_lc5_councillor <- list(h6a_1,
                              h6a_2,
                              h6a_3,
                              h6a_4,
                              h6b_1,
                              h6b_2,
                              h6b_3,
                              h6b_4,
                              h6a_1_cov,
                              h6a_2_cov,
                              h6a_3_cov,
                              h6a_4_cov,
                              h6b_1_cov,
                              h6b_2_cov,
                              h6b_3_cov,
                              h6b_4_cov
)

h6_out_labels <- c("h6a_1",
                   "h6a_2",
                   "h6a_3",
                   "h6a_4",
                   "h6b_1",
                   "h6b_2",
                   "h6b_3",
                   "h6b_4",
                   "h6a_1_cov",
                   "h6a_2_cov",
                   "h6a_3_cov",
                   "h6a_4_cov",
                   "h6b_1_cov",
                   "h6b_2_cov",
                   "h6b_3_cov",
                   "h6b_4_cov"
)

names(h6_out_lc5_councillor) <- h6_out_labels


##H10: Vote Buying

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(received.gifts = 
           case_when(office=="lc5.chair.inc.vote" ~ lc5.received.gifts,
                     office=="lc5.councillor.inc.vote" ~ lc5.received.gifts,
                     office=="lc3.chair.inc.vote" ~ lc3.received.gifts,
                     office=="lc3.councillor.inc.vote" ~ lc3.received.gifts))

#subsetting on LC5 Chair data
keep <- ifelse(dta$office=="lc5.councillor.inc.vote" & dta$lc5.councillor.competitive==1 & dta$lc5.councillor.party.switch==0, 1, 0)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(received.gifts = factor(received.gifts,  
                                 labels = c("no gifts", "received gifts")))

#pooled regression, looping through base levels
l <- rev(levels(dta$received.gifts)) #received gifts & no gifts

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i), h10b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i,"_cov"), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i,"_cov"), h10b.ps.pooled)
}

#collecting model outputs:
h10_out_lc5_councillor <- list(h10a_1,
                               h10a_2,
                               h10b_1,
                               h10b_2,
                               h10a_1_cov,
                               h10a_2_cov,
                               h10b_1_cov,
                               h10b_2_cov
)

h10_out_labels <- c("h10a_1",
                    "h10a_2",
                    "h10b_1",
                    "h10b_2",
                    "h10a_1_cov",
                    "h10a_2_cov",
                    "h10b_1_cov",
                    "h10b_2_cov"
)

names(h10_out_lc5_councillor) <- h10_out_labels

```

```{r figS10-cate-lc5-councillor, fig.height=8, fig.width = 10}

hte_out <- c(h2_out_lc5_councillor,
             h5_out_lc5_councillor,
             h6_out_lc5_councillor,
             h10_out_lc5_councillor) #have to run primary hypotheses blocks to generate these outputs
# names(hte_out) <- c(h2_out_labels,h5_out_labels,h6_out_labels,h8_out_labels,h9_out_labels,h10_out_labels)

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

#h2=inc.vote
#h5=inc.vote
#h6=inc.vote
#h8=turnout
#h9=inc.vote
#h10=inc.vote

results_out <- tibble(names(hte_out)) %>% rename(label="names(hte_out)") %>%
  mutate(hypothesis=word(label,1,sep = "\\_")) %>%
  mutate(outcome_var=ifelse(hypothesis %in% c("h8a","h8b"),"turnout","inc.vote")) %>%
  mutate(mod_label = case_when(label=="h2a_4" ~ "+4", 
                               label=="h2a_3" ~ "+3",
                               label=="h2a_2" ~ "+2", 
                               label=="h2a_1" ~ "+1",
                               label=="h2a_0" ~ "same",
                               label=="h2b_0" ~ "same", 
                               label=="h2b_1" ~ "-1",
                               label=="h2b_2" ~ "-2", 
                               label=="h2b_3" ~ "-3",
                               label=="h2b_4" ~ "-4",
                               label=="h5a_4" ~ "Very\nUncertain", 
                               label=="h5a_3" ~ "Not\nCertain",
                               label=="h5a_2" ~ "Certain", 
                               label=="h5a_1" ~ "Very\nCertain",
                               label=="h5b_4" ~ "Very\nUncertain", 
                               label=="h5b_3" ~ "Not\nCertain",
                               label=="h5b_2" ~ "Certain", 
                               label=="h5b_1" ~ "Very\nCertain",
                               label=="h6a_1" ~ "Not\nImportant", 
                               label=="h6a_2" ~ "Not Very\nImportant",
                               label=="h6a_3" ~ "Somewhat\nImportant", 
                               label=="h6a_4" ~ "Very\nImportant",
                               label=="h6b_1" ~ "Not\nImportant", 
                               label=="h6b_2" ~ "Not Very\nImportant",
                               label=="h6b_3" ~ "Somewhat\nImportant", 
                               label=="h6b_4" ~ "Very\nImportant",
                               label=="h8a_2" ~ "Aligned", 
                               label=="h8a_1" ~ "Not Aligned",
                               label=="h8b_2" ~ "Aligned", 
                               label=="h8b_1" ~ "Not Aligned",
                               label=="h10a_1" ~ "Received Gifts", 
                               label=="h10a_2" ~ "No Gifts",
                               label=="h10b_1" ~ "Received Gifts", 
                               label=="h10b_2" ~ "No Gifts",
                               label=="h2a_4_cov" ~ "+4", 
                               label=="h2a_3_cov" ~ "+3",
                               label=="h2a_2_cov" ~ "+2", 
                               label=="h2a_1_cov" ~ "+1",
                               label=="h2a_0_cov" ~ "same",
                               label=="h2b_0_cov" ~ "same", 
                               label=="h2b_1_cov" ~ "-1",
                               label=="h2b_2_cov" ~ "-2", 
                               label=="h2b_3_cov" ~ "-3",
                               label=="h2b_4_cov" ~ "-4",
                               label=="h5a_4_cov" ~ "Very\nUncertain", 
                               label=="h5a_3_cov" ~ "Not\nCertain",
                               label=="h5a_2_cov" ~ "Certain", 
                               label=="h5a_1_cov" ~ "Very\nCertain",
                               label=="h5b_4_cov" ~ "Very\nUncertain", 
                               label=="h5b_3_cov" ~ "Not\nCertain",
                               label=="h5b_2_cov" ~ "Certain", 
                               label=="h5b_1_cov" ~ "Very\nCertain",
                               label=="h6a_1_cov" ~ "Not\nImportant", 
                               label=="h6a_2_cov" ~ "Not Very\nImportant",
                               label=="h6a_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6a_4_cov" ~ "Very\nImportant",
                               label=="h6b_1_cov" ~ "Not\nImportant", 
                               label=="h6b_2_cov" ~ "Not Very\nImportant",
                               label=="h6b_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6b_4_cov" ~ "Very\nImportant",
                               label=="h8a_2_cov" ~ "Aligned", 
                               label=="h8a_1_cov" ~ "Not Aligned",
                               label=="h8b_2_cov" ~ "Aligned", 
                               label=="h8b_1_cov" ~ "Not Aligned",
                               label=="h9a_1_cov" ~ "same tribe", 
                               label=="h9a_2_cov" ~ "not same tribe",
                               label=="h9b_1_cov" ~ "same tribe", 
                               label=="h9b_2_cov" ~ "not same tribe",
                               label=="h10a_1_cov" ~ "Received Gifts", 
                               label=="h10a_2_cov" ~ "No Gifts",
                               label=="h10b_1_cov" ~ "Received Gifts", 
                               label=="h10b_2_cov" ~ "No Gifts"
  )) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(hte_out, coef_grab)) %>%
  mutate(se=sapply(hte_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h2a <- results_out %>%
  filter(hypothesis=="h2a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","+1","+2","+3","+4")))

h2a <- ggplot(data = results_out_h2a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2a$mod_label),
                   labels = unique(results_out_h2a$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.5) +
  ggtitle("Difference w/ Priors, Good News")


results_out_h2b <- results_out %>%
  filter(hypothesis=="h2b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","-1","-2","-3","-4")))

h2b <- ggplot(data = results_out_h2b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2b$mod_label),
                   labels = unique(results_out_h2b$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.5) +
  ggtitle("Difference w/ Priors, Bad News")


results_out_h5a <- results_out %>%
  filter(hypothesis=="h5a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5a <- ggplot(data = results_out_h5a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5a$mod_label),
                   labels = unique(results_out_h5a$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.25) +
  ggtitle("Certainty, Good News")

results_out_h5b <- results_out %>%
  filter(hypothesis=="h5b") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5b <- ggplot(data = results_out_h5b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5b$mod_label),
                   labels = unique(results_out_h5b$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.25) +
  ggtitle("Certainty, Bad News")


results_out_h6a <- results_out %>%
  filter(hypothesis=="h6a") # %>%
#mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6a <- ggplot(data = results_out_h6a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6a$mod_label),
                   labels = unique(results_out_h6a$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.3,0.4) +
  ggtitle("Importance, Good News")

results_out_h6b <- results_out %>%
  filter(hypothesis=="h6b") # %>%
#mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6b <- ggplot(data = results_out_h6b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6b$mod_label),
                   labels = unique(results_out_h6b$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.3,0.4) +
  ggtitle("Importance, Bad News")

results_out_h10a <- results_out %>%
  filter(hypothesis=="h10a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10a <- ggplot(data = results_out_h10a,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10a$mod_label),
                   labels = unique(results_out_h10a$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.15,0.07) +
  ggtitle("Vote Buying, Good News")

results_out_h10b <- results_out %>%
  filter(hypothesis=="h10b")  %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10b <- ggplot(data = results_out_h10b,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10b$mod_label),
                   labels = unique(results_out_h10b$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.15,0.07) +
  ggtitle("Vote Buying, Bad News")

# pdf(file="lc5-councillor-hte-cov-200626.pdf", height=8, width = 10)
ggarrange(h2a,h2b,h5a,h5b,h6a,h6b,h10a,h10b,
          ncol = 2, nrow = 4,
          labels = c("A","B","C","D","E","F","K","L"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```

## Figure S11: Conditional effects of treatment with good and bad news subgroups for LC3 Chairperson elections

```{r lc3-chair-cate-analysis}

##H2: Difference with priors

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

#keeping only lc3 Chair
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0, 1, 0)

#Subsetting execution:
keep <- ifelse(dta$finalps.prior!=round(dta$finalps.prior), 0, keep) #removing imputed values of the moderator

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta$finalps.prior.diff <- as.factor(dta$finalps.prior.diff)
l <- levels(dta$finalps.prior.diff) #-4 to 4

#without covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i]), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", "")),
         h2b.ps.pooled)
}

#with covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i],"_cov"), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", ""),"_cov"),
         h2b.ps.pooled)
}

#collecting both with/without cov output into list:
h2_out_lc3_chair <- list(h2a_0,
                         h2a_1,
                         h2a_2,
                         h2a_3,
                         h2a_4,
                         h2b_0,
                         h2b_1,
                         h2b_2,
                         h2b_3,
                         h2b_4,
                         h2a_0_cov,
                         h2a_1_cov,
                         h2a_2_cov,
                         h2a_3_cov,
                         h2a_4_cov,
                         h2b_0_cov,
                         h2b_1_cov,
                         h2b_2_cov,
                         h2b_3_cov,
                         h2b_4_cov
)

h2_out_labels <- c("h2a_0",
                   "h2a_1",
                   "h2a_2",
                   "h2a_3",
                   "h2a_4",
                   "h2b_0",
                   "h2b_1",
                   "h2b_2",
                   "h2b_3",
                   "h2b_4",
                   "h2a_0_cov",
                   "h2a_1_cov",
                   "h2a_2_cov",
                   "h2a_3_cov",
                   "h2a_4_cov",
                   "h2b_0_cov",
                   "h2b_1_cov",
                   "h2b_2_cov",
                   "h2b_3_cov",
                   "h2b_4_cov"
)

names(h2_out_lc3_chair) <- h2_out_labels


##H5: Uncertainty

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(finalps.certainty = factor(finalps.certainty,
                                    levels=rev(c("very_uncertain","not_certain",
                                                 "certain","very_certain"))))

#subsetting on pooled data
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$finalps.certainty) #very_uncertain to certain

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i), h5b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i,"_cov"), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i,"_cov"), h5b.ps.pooled)
}

h5_out_lc3_chair <- list(h5a_1,
                         h5a_2,
                         h5a_3,
                         h5a_4,
                         h5b_1,
                         h5b_2,
                         h5b_3,
                         h5b_4,
                         h5a_1_cov,
                         h5a_2_cov,
                         h5a_3_cov,
                         h5a_4_cov,
                         h5b_1_cov,
                         h5b_2_cov,
                         h5b_3_cov,
                         h5b_4_cov
)

h5_out_labels <- c("h5a_1",
                   "h5a_2",
                   "h5a_3",
                   "h5a_4",
                   "h5b_1",
                   "h5b_2",
                   "h5b_3",
                   "h5b_4",
                   "h5a_1_cov",
                   "h5a_2_cov",
                   "h5a_3_cov",
                   "h5a_4_cov",
                   "h5b_1_cov",
                   "h5b_2_cov",
                   "h5b_3_cov",
                   "h5b_4_cov"
)

names(h5_out_lc3_chair) <- h5_out_labels


##H6: Importance

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(ps.importance = factor(ps.importance, levels=c("very_important","somewhat_impor","not_very_impor","not_important")))

#subsetting on lc3 Chair
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- rev(levels(dta$ps.importance)) #not_important to very_important

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i), h6b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i,"_cov"), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i,"_cov"), h6b.ps.pooled)
}

#collecting model outputs:
h6_out_lc3_chair <- list(h6a_1,
                         h6a_2,
                         h6a_3,
                         h6a_4,
                         h6b_1,
                         h6b_2,
                         h6b_3,
                         h6b_4,
                         h6a_1_cov,
                         h6a_2_cov,
                         h6a_3_cov,
                         h6a_4_cov,
                         h6b_1_cov,
                         h6b_2_cov,
                         h6b_3_cov,
                         h6b_4_cov
)

h6_out_labels <- c("h6a_1",
                   "h6a_2",
                   "h6a_3",
                   "h6a_4",
                   "h6b_1",
                   "h6b_2",
                   "h6b_3",
                   "h6b_4",
                   "h6a_1_cov",
                   "h6a_2_cov",
                   "h6a_3_cov",
                   "h6a_4_cov",
                   "h6b_1_cov",
                   "h6b_2_cov",
                   "h6b_3_cov",
                   "h6b_4_cov"
)

names(h6_out_lc3_chair) <- h6_out_labels


##H8: Alignment and turnout

#pooling data
dta <- data %>% gather(e_level,turnout,c("lc5.turnout","lc3.turnout")) %>%
  mutate(turnout.intent = 
           case_when(e_level=="lc5.turnout" ~ lc5.turnout.intent,
                     e_level=="lc3.turnout" ~ lc3.turnout.intent)) %>%
  mutate(aligned.inc = case_when(
    e_level=="lc5.turnout" ~ aligned.lc5.chair.inc,
    e_level=="lc3.turnout" ~ aligned.lc3.chair.inc)) %>%
  mutate(aligned.inc = factor(aligned.inc, labels = c("not_aligned", "aligned"))) 

#subsetting on lc3 election
keep <- ifelse(dta$e_level=="lc3.turnout", 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$aligned.inc) #not_aligned & aligned

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))
  
  h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
  assign(paste0("h8a_",i), h8a.ps.pooled)
  
  h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h8b_",i), h8b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(aligned.inc = relevel(aligned.inc, ref=l[i]))
  
  h8a.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.good.noNA==1))
  assign(paste0("h8a_",i,"_cov"), h8a.ps.pooled)
  
  h8b.ps.pooled <- felm(turnout ~ ps.treat*aligned.inc + turnout.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h8b_",i,"_cov"), h8b.ps.pooled)
}

#collecting model outputs:
h8_out_lc3 <- list(h8a_1,
                   h8a_2,
                   h8b_1,
                   h8b_2,
                   h8a_1_cov,
                   h8a_2_cov,
                   h8b_1_cov,
                   h8b_2_cov
)

h8_out_labels <- c("h8a_1",
                   "h8a_2",
                   "h8b_1",
                   "h8b_2",
                   "h8a_1_cov",
                   "h8a_2_cov",
                   "h8b_1_cov",
                   "h8b_2_cov"
)

names(h8_out_lc3) <- h8_out_labels


##H10: Vote buying

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(received.gifts = 
           case_when(office=="lc5.chair.inc.vote" ~ lc5.received.gifts,
                     office=="lc5.councillor.inc.vote" ~ lc5.received.gifts,
                     office=="lc3.chair.inc.vote" ~ lc3.received.gifts,
                     office=="lc3.councillor.inc.vote" ~ lc3.received.gifts))

#subsetting on lc3 Chair data
keep <- ifelse(dta$office=="lc3.chair.inc.vote" & dta$lc3.chair.competitive==1 & dta$lc3.chair.party.switch==0, 1, 0)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(received.gifts = factor(received.gifts,  
                                 labels = c("no gifts", "received gifts")))

#pooled regression, looping through base levels
l <- rev(levels(dta$received.gifts)) #received gifts & no gifts

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i), h10b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i,"_cov"), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i,"_cov"), h10b.ps.pooled)
}

#collecting model outputs:
h10_out_lc3_chair <- list(h10a_1,
                          h10a_2,
                          h10b_1,
                          h10b_2,
                          h10a_1_cov,
                          h10a_2_cov,
                          h10b_1_cov,
                          h10b_2_cov
)

h10_out_labels <- c("h10a_1",
                    "h10a_2",
                    "h10b_1",
                    "h10b_2",
                    "h10a_1_cov",
                    "h10a_2_cov",
                    "h10b_1_cov",
                    "h10b_2_cov"
)

names(h10_out_lc3_chair) <- h10_out_labels

```

```{r figS11-cate-lc3-chair, fig.height=10, fig.width = 10}

hte_out <- c(h2_out_lc3_chair,
             h5_out_lc3_chair,
             h6_out_lc3_chair,
             h8_out_lc3,
             h10_out_lc3_chair) 

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

#h2=inc.vote
#h5=inc.vote
#h6=inc.vote
#h8=turnout
#h9=inc.vote
#h10=inc.vote

results_out <- tibble(names(hte_out)) %>% rename(label="names(hte_out)") %>%
  mutate(hypothesis=word(label,1,sep = "\\_")) %>%
  mutate(outcome_var=ifelse(hypothesis %in% c("h8a","h8b"),"turnout","inc.vote")) %>%
  mutate(mod_label = case_when(label=="h2a_4" ~ "+4", 
                               label=="h2a_3" ~ "+3",
                               label=="h2a_2" ~ "+2", 
                               label=="h2a_1" ~ "+1",
                               label=="h2a_0" ~ "same",
                               label=="h2b_0" ~ "same", 
                               label=="h2b_1" ~ "-1",
                               label=="h2b_2" ~ "-2", 
                               label=="h2b_3" ~ "-3",
                               label=="h2b_4" ~ "-4",
                               label=="h5a_4" ~ "Very\nUncertain", 
                               label=="h5a_3" ~ "Not\nCertain",
                               label=="h5a_2" ~ "Certain", 
                               label=="h5a_1" ~ "Very\nCertain",
                               label=="h5b_4" ~ "Very\nUncertain", 
                               label=="h5b_3" ~ "Not\nCertain",
                               label=="h5b_2" ~ "Certain", 
                               label=="h5b_1" ~ "Very\nCertain",
                               label=="h6a_1" ~ "Not\nImportant", 
                               label=="h6a_2" ~ "Not Very\nImportant",
                               label=="h6a_3" ~ "Somewhat\nImportant", 
                               label=="h6a_4" ~ "Very\nImportant",
                               label=="h6b_1" ~ "Not\nImportant", 
                               label=="h6b_2" ~ "Not Very\nImportant",
                               label=="h6b_3" ~ "Somewhat\nImportant", 
                               label=="h6b_4" ~ "Very\nImportant",
                               label=="h8a_2" ~ "Aligned", 
                               label=="h8a_1" ~ "Not Aligned",
                               label=="h8b_2" ~ "Aligned", 
                               label=="h8b_1" ~ "Not Aligned",
                               label=="h10a_1" ~ "Received Gifts", 
                               label=="h10a_2" ~ "No Gifts",
                               label=="h10b_1" ~ "Received Gifts", 
                               label=="h10b_2" ~ "No Gifts",
                               label=="h2a_4_cov" ~ "+4", 
                               label=="h2a_3_cov" ~ "+3",
                               label=="h2a_2_cov" ~ "+2", 
                               label=="h2a_1_cov" ~ "+1",
                               label=="h2a_0_cov" ~ "same",
                               label=="h2b_0_cov" ~ "same", 
                               label=="h2b_1_cov" ~ "-1",
                               label=="h2b_2_cov" ~ "-2", 
                               label=="h2b_3_cov" ~ "-3",
                               label=="h2b_4_cov" ~ "-4",
                               label=="h5a_4_cov" ~ "Very\nUncertain", 
                               label=="h5a_3_cov" ~ "Not\nCertain",
                               label=="h5a_2_cov" ~ "Certain", 
                               label=="h5a_1_cov" ~ "Very\nCertain",
                               label=="h5b_4_cov" ~ "Very\nUncertain", 
                               label=="h5b_3_cov" ~ "Not\nCertain",
                               label=="h5b_2_cov" ~ "Certain", 
                               label=="h5b_1_cov" ~ "Very\nCertain",
                               label=="h6a_1_cov" ~ "Not\nImportant", 
                               label=="h6a_2_cov" ~ "Not Very\nImportant",
                               label=="h6a_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6a_4_cov" ~ "Very\nImportant",
                               label=="h6b_1_cov" ~ "Not\nImportant", 
                               label=="h6b_2_cov" ~ "Not Very\nImportant",
                               label=="h6b_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6b_4_cov" ~ "Very\nImportant",
                               label=="h8a_2_cov" ~ "Aligned", 
                               label=="h8a_1_cov" ~ "Not Aligned",
                               label=="h8b_2_cov" ~ "Aligned", 
                               label=="h8b_1_cov" ~ "Not Aligned",
                               label=="h9a_1_cov" ~ "same tribe", 
                               label=="h9a_2_cov" ~ "not same tribe",
                               label=="h9b_1_cov" ~ "same tribe", 
                               label=="h9b_2_cov" ~ "not same tribe",
                               label=="h10a_1_cov" ~ "Received Gifts", 
                               label=="h10a_2_cov" ~ "No Gifts",
                               label=="h10b_1_cov" ~ "Received Gifts", 
                               label=="h10b_2_cov" ~ "No Gifts"
  )) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(hte_out, coef_grab)) %>%
  mutate(se=sapply(hte_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h2a <- results_out %>%
  filter(hypothesis=="h2a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","+1","+2","+3","+4")))

h2a <- ggplot(data = results_out_h2a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2a$mod_label),
                   labels = unique(results_out_h2a$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.35,0.3) +
  ggtitle("Difference w/ Priors, Good News")


results_out_h2b <- results_out %>%
  filter(hypothesis=="h2b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","-1","-2","-3","-4")))

h2b <- ggplot(data = results_out_h2b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2b$mod_label),
                   labels = unique(results_out_h2b$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.35,0.3) +
  ggtitle("Difference w/ Priors, Bad News")


results_out_h5a <- results_out %>%
  filter(hypothesis=="h5a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5a <- ggplot(data = results_out_h5a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5a$mod_label),
                   labels = unique(results_out_h5a$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.2) +
  ggtitle("Certainty, Good News")

results_out_h5b <- results_out %>%
  filter(hypothesis=="h5b") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5b <- ggplot(data = results_out_h5b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5b$mod_label),
                   labels = unique(results_out_h5b$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.2) +
  ggtitle("Certainty, Bad News")


results_out_h6a <- results_out %>%
  filter(hypothesis=="h6a") # %>%
#mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6a <- ggplot(data = results_out_h6a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6a$mod_label),
                   labels = unique(results_out_h6a$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.35,0.3) +
  ggtitle("Importance, Good News")

results_out_h6b <- results_out %>%
  filter(hypothesis=="h6b") # %>%

h6b <- ggplot(data = results_out_h6b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6b$mod_label),
                   labels = unique(results_out_h6b$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.35,0.3) +
  ggtitle("Importance, Bad News")


results_out_h8a <- results_out %>%
  filter(hypothesis=="h8a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Not Aligned","Aligned")))

h8a <- ggplot(data = results_out_h8a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h8a$mod_label),
                   labels = unique(results_out_h8a$mod_label)) +
  labs(x = "Alignment w/ Incumbent Chair", 
       y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.05,0.05) +
  ggtitle("Alignment, Good News")

results_out_h8b <- results_out %>%
  filter(hypothesis=="h8b") # %>%
#mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h8b <- ggplot(data = results_out_h8b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h8b$mod_label),
                   labels = unique(results_out_h8b$mod_label)) +
  labs(x = "Alignment w/ Incumbent Chair", 
       y = "Treatment Effect \n(Turnout)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.05,0.05) +
  ggtitle("Alignment, Bad News")


results_out_h10a <- results_out %>%
  filter(hypothesis=="h10a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10a <- ggplot(data = results_out_h10a,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10a$mod_label),
                   labels = unique(results_out_h10a$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.06,0.16) +
  ggtitle("Vote Buying, Good News")

results_out_h10b <- results_out %>%
  filter(hypothesis=="h10b")  %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10b <- ggplot(data = results_out_h10b,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10b$mod_label),
                   labels = unique(results_out_h10b$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.06,0.16) +
  ggtitle("Vote Buying, Bad News")

# pdf(file="lc3-chair-hte-cov-200629.pdf", height=10, width = 10)
ggarrange(h2a,h2b,h5a,h5b,h6a,h6b,h8a,h8b,h10a,h10b,
          ncol = 2, nrow = 5,
          labels = c("A","B","C","D","E","F","G","H","K","L"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```

## Figure S12: Conditional effects of treatment with good and bad news subgroups for LC3 Councillor elections

```{r lc3-councillor-cate-analysis}

##H2: Difference in prior beliefs

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent))

#keeping only lc3 Councillor
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0, 1, 0)

#Subsetting execution:
keep <- ifelse(dta$finalps.prior!=round(dta$finalps.prior), 0, keep) #removing imputed values of the moderator

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta$finalps.prior.diff <- as.factor(dta$finalps.prior.diff)
l <- levels(dta$finalps.prior.diff) #-4 to 4

#without covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i]), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", "")),
         h2b.ps.pooled)
}

#with covariates:
for (i in 5:9){ #levels relevant to good news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h2a_",l[i],"_cov"), h2a.ps.pooled)
}

for (i in 5:1){ #levels relevant to bad news
  dta <- dta %>% mutate(finalps.prior.diff = relevel(finalps.prior.diff, ref=l[i]))
  
  h2b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.prior.diff + finalps.prior + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h2b_",str_replace_all(as.character(l[i]), "[[:punct:]]", ""),"_cov"),
         h2b.ps.pooled)
}


#collecting both with/without cov output into list:
h2_out_lc3_councillor <- list(h2a_0,
                              h2a_1,
                              h2a_2,
                              h2a_3,
                              h2a_4,
                              h2b_0,
                              h2b_1,
                              h2b_2,
                              h2b_3,
                              h2b_4,
                              h2a_0_cov,
                              h2a_1_cov,
                              h2a_2_cov,
                              h2a_3_cov,
                              h2a_4_cov,
                              h2b_0_cov,
                              h2b_1_cov,
                              h2b_2_cov,
                              h2b_3_cov,
                              h2b_4_cov
)

h2_out_labels <- c("h2a_0",
                   "h2a_1",
                   "h2a_2",
                   "h2a_3",
                   "h2a_4",
                   "h2b_0",
                   "h2b_1",
                   "h2b_2",
                   "h2b_3",
                   "h2b_4",
                   "h2a_0_cov",
                   "h2a_1_cov",
                   "h2a_2_cov",
                   "h2a_3_cov",
                   "h2a_4_cov",
                   "h2b_0_cov",
                   "h2b_1_cov",
                   "h2b_2_cov",
                   "h2b_3_cov",
                   "h2b_4_cov"
)

names(h2_out_lc3_councillor) <- h2_out_labels


##H5: Uncertainty

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(finalps.certainty = factor(finalps.certainty,
                                    levels=rev(c("very_uncertain","not_certain",
                                                 "certain","very_certain"))))

#subsetting on pooled data
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- levels(dta$finalps.certainty) #very_uncertain to certain

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i), h5b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(finalps.certainty = relevel(finalps.certainty, ref=l[i]))
  
  h5a.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h5a_",i,"_cov"), h5a.ps.pooled)
  
  h5b.ps.pooled <- felm(inc.vote ~ ps.treat*finalps.certainty + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h5b_",i,"_cov"), h5b.ps.pooled)
}

h5_out_lc3_councillor <- list(h5a_1,
                              h5a_2,
                              h5a_3,
                              h5a_4,
                              h5b_1,
                              h5b_2,
                              h5b_3,
                              h5b_4,
                              h5a_1_cov,
                              h5a_2_cov,
                              h5a_3_cov,
                              h5a_4_cov,
                              h5b_1_cov,
                              h5b_2_cov,
                              h5b_3_cov,
                              h5b_4_cov
)

h5_out_labels <- c("h5a_1",
                   "h5a_2",
                   "h5a_3",
                   "h5a_4",
                   "h5b_1",
                   "h5b_2",
                   "h5b_3",
                   "h5b_4",
                   "h5a_1_cov",
                   "h5a_2_cov",
                   "h5a_3_cov",
                   "h5a_4_cov",
                   "h5b_1_cov",
                   "h5b_2_cov",
                   "h5b_3_cov",
                   "h5b_4_cov"
)

names(h5_out_lc3_councillor) <- h5_out_labels


##H6: Importance

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(ps.importance = factor(ps.importance, levels=c("very_important","somewhat_impor","not_very_impor","not_important")))

#subsetting on lc3 councillor:
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0, 1, 0)
keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
l <- rev(levels(dta$ps.importance)) #not_important to very_important

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i), h6b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(ps.importance = relevel(ps.importance, ref=l[i]))
  
  h6a.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.good.noNA==1))
  assign(paste0("h6a_",i,"_cov"), h6a.ps.pooled)
  
  h6b.ps.pooled <- felm(inc.vote ~ ps.treat*ps.importance + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                        data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h6b_",i,"_cov"), h6b.ps.pooled)
}

#collecting model outputs:
h6_out_lc3_councillor <- list(h6a_1,
                              h6a_2,
                              h6a_3,
                              h6a_4,
                              h6b_1,
                              h6b_2,
                              h6b_3,
                              h6b_4,
                              h6a_1_cov,
                              h6a_2_cov,
                              h6a_3_cov,
                              h6a_4_cov,
                              h6b_1_cov,
                              h6b_2_cov,
                              h6b_3_cov,
                              h6b_4_cov
)

h6_out_labels <- c("h6a_1",
                   "h6a_2",
                   "h6a_3",
                   "h6a_4",
                   "h6b_1",
                   "h6b_2",
                   "h6b_3",
                   "h6b_4",
                   "h6a_1_cov",
                   "h6a_2_cov",
                   "h6a_3_cov",
                   "h6a_4_cov",
                   "h6b_1_cov",
                   "h6b_2_cov",
                   "h6b_3_cov",
                   "h6b_4_cov"
)

names(h6_out_lc3_councillor) <- h6_out_labels


##H10: Vote Buying

#pooling data
dta <- data %>% gather(office,inc.vote,c("lc5.chair.inc.vote","lc5.councillor.inc.vote","lc3.chair.inc.vote","lc3.councillor.inc.vote")) %>%
  mutate(intent = case_when(office=="lc5.chair.inc.vote" ~ lc5.chair.intent,
                            office=="lc5.councillor.inc.vote" ~ lc5.councillor.intent,
                            office=="lc3.chair.inc.vote" ~ lc3.chair.intent,
                            office=="lc3.councillor.inc.vote" ~ lc5.councillor.intent)) %>%
  mutate(received.gifts = 
           case_when(office=="lc5.chair.inc.vote" ~ lc5.received.gifts,
                     office=="lc5.councillor.inc.vote" ~ lc5.received.gifts,
                     office=="lc3.chair.inc.vote" ~ lc3.received.gifts,
                     office=="lc3.councillor.inc.vote" ~ lc3.received.gifts))

#subsetting on lc3 Chair data
keep <- ifelse(dta$office=="lc3.councillor.inc.vote" & dta$lc3.councillor.competitive==1 & dta$lc3.councillor.party.switch==0, 1, 0)

keep[is.na(keep)] <- 0
dta <- dta[keep==1,]

#pooled regression, looping through base levels
dta <- dta %>%
  mutate(received.gifts = factor(received.gifts,  
                                 labels = c("no gifts", "received gifts")))

#pooled regression, looping through base levels
l <- rev(levels(dta$received.gifts)) #received gifts & no gifts

#without covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i), h10b.ps.pooled)
}

#with covariates:
for (i in 1:length(l)){
  dta <- dta %>% mutate(received.gifts = relevel(received.gifts, ref=l[i]))
  
  h10a.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.good.noNA==1))
  assign(paste0("h10a_",i,"_cov"), h10a.ps.pooled)
  
  h10b.ps.pooled <- felm(inc.vote ~ ps.treat*received.gifts + intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | id.cleaned, 
                         data=filter(dta, ps.bad.noNA==1))
  assign(paste0("h10b_",i,"_cov"), h10b.ps.pooled)
}

#collecting model outputs:
h10_out_lc3_councillor <- list(h10a_1,
                               h10a_2,
                               h10b_1,
                               h10b_2,
                               h10a_1_cov,
                               h10a_2_cov,
                               h10b_1_cov,
                               h10b_2_cov
)

h10_out_labels <- c("h10a_1",
                    "h10a_2",
                    "h10b_1",
                    "h10b_2",
                    "h10a_1_cov",
                    "h10a_2_cov",
                    "h10b_1_cov",
                    "h10b_2_cov"
)

names(h10_out_lc3_councillor) <- h10_out_labels

```

```{r figS12-cate-lc3-councillor, fig.height=8, fig.width = 10}

hte_out <- c(h2_out_lc3_councillor,
             h5_out_lc3_councillor,
             h6_out_lc3_councillor,
             h10_out_lc3_councillor) 

coef_grab <- function(x){
  coef(x)[names(coef(x))=="ps.treat"]
}

se_grab <- function(x){
  x$cse[names(x$cse)=="ps.treat"]
}

#h2=inc.vote
#h5=inc.vote
#h6=inc.vote
#h8=turnout
#h9=inc.vote
#h10=inc.vote

results_out <- tibble(names(hte_out)) %>% rename(label="names(hte_out)") %>%
  mutate(hypothesis=word(label,1,sep = "\\_")) %>%
  mutate(outcome_var=ifelse(hypothesis %in% c("h8a","h8b"),"turnout","inc.vote")) %>%
  mutate(mod_label = case_when(label=="h2a_4" ~ "+4", 
                               label=="h2a_3" ~ "+3",
                               label=="h2a_2" ~ "+2", 
                               label=="h2a_1" ~ "+1",
                               label=="h2a_0" ~ "same",
                               label=="h2b_0" ~ "same", 
                               label=="h2b_1" ~ "-1",
                               label=="h2b_2" ~ "-2", 
                               label=="h2b_3" ~ "-3",
                               label=="h2b_4" ~ "-4",
                               label=="h5a_4" ~ "Very\nUncertain", 
                               label=="h5a_3" ~ "Not\nCertain",
                               label=="h5a_2" ~ "Certain", 
                               label=="h5a_1" ~ "Very\nCertain",
                               label=="h5b_4" ~ "Very\nUncertain", 
                               label=="h5b_3" ~ "Not\nCertain",
                               label=="h5b_2" ~ "Certain", 
                               label=="h5b_1" ~ "Very\nCertain",
                               label=="h6a_1" ~ "Not\nImportant", 
                               label=="h6a_2" ~ "Not Very\nImportant",
                               label=="h6a_3" ~ "Somewhat\nImportant", 
                               label=="h6a_4" ~ "Very\nImportant",
                               label=="h6b_1" ~ "Not\nImportant", 
                               label=="h6b_2" ~ "Not Very\nImportant",
                               label=="h6b_3" ~ "Somewhat\nImportant", 
                               label=="h6b_4" ~ "Very\nImportant",
                               label=="h8a_2" ~ "Aligned", 
                               label=="h8a_1" ~ "Not Aligned",
                               label=="h8b_2" ~ "Aligned", 
                               label=="h8b_1" ~ "Not Aligned",
                               label=="h10a_1" ~ "Received Gifts", 
                               label=="h10a_2" ~ "No Gifts",
                               label=="h10b_1" ~ "Received Gifts", 
                               label=="h10b_2" ~ "No Gifts",
                               label=="h2a_4_cov" ~ "+4", 
                               label=="h2a_3_cov" ~ "+3",
                               label=="h2a_2_cov" ~ "+2", 
                               label=="h2a_1_cov" ~ "+1",
                               label=="h2a_0_cov" ~ "same",
                               label=="h2b_0_cov" ~ "same", 
                               label=="h2b_1_cov" ~ "-1",
                               label=="h2b_2_cov" ~ "-2", 
                               label=="h2b_3_cov" ~ "-3",
                               label=="h2b_4_cov" ~ "-4",
                               label=="h5a_4_cov" ~ "Very\nUncertain", 
                               label=="h5a_3_cov" ~ "Not\nCertain",
                               label=="h5a_2_cov" ~ "Certain", 
                               label=="h5a_1_cov" ~ "Very\nCertain",
                               label=="h5b_4_cov" ~ "Very\nUncertain", 
                               label=="h5b_3_cov" ~ "Not\nCertain",
                               label=="h5b_2_cov" ~ "Certain", 
                               label=="h5b_1_cov" ~ "Very\nCertain",
                               label=="h6a_1_cov" ~ "Not\nImportant", 
                               label=="h6a_2_cov" ~ "Not Very\nImportant",
                               label=="h6a_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6a_4_cov" ~ "Very\nImportant",
                               label=="h6b_1_cov" ~ "Not\nImportant", 
                               label=="h6b_2_cov" ~ "Not Very\nImportant",
                               label=="h6b_3_cov" ~ "Somewhat\nImportant", 
                               label=="h6b_4_cov" ~ "Very\nImportant",
                               label=="h8a_2_cov" ~ "Aligned", 
                               label=="h8a_1_cov" ~ "Not Aligned",
                               label=="h8b_2_cov" ~ "Aligned", 
                               label=="h8b_1_cov" ~ "Not Aligned",
                               label=="h9a_1_cov" ~ "same tribe", 
                               label=="h9a_2_cov" ~ "not same tribe",
                               label=="h9b_1_cov" ~ "same tribe", 
                               label=="h9b_2_cov" ~ "not same tribe",
                               label=="h10a_1_cov" ~ "Received Gifts", 
                               label=="h10a_2_cov" ~ "No Gifts",
                               label=="h10b_1_cov" ~ "Received Gifts", 
                               label=="h10b_2_cov" ~ "No Gifts"
  )) %>%
  mutate(covariates=endsWith(label,"cov")) %>%
  mutate(covariates=ifelse(covariates,"Yes","No")) %>%
  mutate(estimate=sapply(hte_out, coef_grab)) %>%
  mutate(se=sapply(hte_out,se_grab)) %>%
  mutate(ci_90_lower=estimate+qnorm(0.05)*se) %>%
  mutate(ci_90_upper=estimate+qnorm(0.95)*se) %>%
  mutate(ci_95_lower=estimate+qnorm(0.025)*se) %>%
  mutate(ci_95_upper=estimate+qnorm(0.975)*se)

results_out_h2a <- results_out %>%
  filter(hypothesis=="h2a") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","+1","+2","+3","+4")))

h2a <- ggplot(data = results_out_h2a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2a$mod_label),
                   labels = unique(results_out_h2a$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.65) +
  ggtitle("Difference w/ Priors, Good News")


results_out_h2b <- results_out %>%
  filter(hypothesis=="h2b") %>%
  mutate(mod_label = factor(mod_label, levels = c("same","-1","-2","-3","-4")))

h2b <- ggplot(data = results_out_h2b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h2b$mod_label),
                   labels = unique(results_out_h2b$mod_label)) +
  labs(x = "(Actual News - Prior Belief)", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.2,0.65) +
  ggtitle("Difference w/ Priors, Bad News")


results_out_h5a <- results_out %>%
  filter(hypothesis=="h5a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5a <- ggplot(data = results_out_h5a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5a$mod_label),
                   labels = unique(results_out_h5a$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.55,0.4) +
  ggtitle("Certainty, Good News")

results_out_h5b <- results_out %>%
  filter(hypothesis=="h5b") %>%
  mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h5b <- ggplot(data = results_out_h5b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h5b$mod_label),
                   labels = unique(results_out_h5b$mod_label)) +
  labs(x = "Level of Certainty about Prior", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.55,0.4) +
  ggtitle("Certainty, Bad News")


results_out_h6a <- results_out %>%
  filter(hypothesis=="h6a") 

h6a <- ggplot(data = results_out_h6a,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6a$mod_label),
                   labels = unique(results_out_h6a$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.6,0.6) +
  ggtitle("Importance, Good News")

results_out_h6b <- results_out %>%
  filter(hypothesis=="h6b") # %>%
#mutate(mod_label = factor(mod_label, levels = c("Very\nCertain","Certain","Not\nCertain","Very\nUncertain")))

h6b <- ggplot(data = results_out_h6b,
              mapping = aes(x = mod_label, y = estimate,
                            ymin = ci_95_lower,
                            ymax = ci_95_upper,
                            group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h6b$mod_label),
                   labels = unique(results_out_h6b$mod_label)) +
  labs(x = "Importance of Public Services", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.6,0.6) +
  ggtitle("Importance, Bad News")

results_out_h10a <- results_out %>%
  filter(hypothesis=="h10a") %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10a <- ggplot(data = results_out_h10a,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10a$mod_label),
                   labels = unique(results_out_h10a$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.15,0.15) +
  ggtitle("Vote Buying, Good News")

results_out_h10b <- results_out %>%
  filter(hypothesis=="h10b")  %>%
  mutate(mod_label = factor(mod_label, levels = c("Received Gifts","No Gifts")))

h10b <- ggplot(data = results_out_h10b,
               mapping = aes(x = mod_label, y = estimate,
                             ymin = ci_95_lower,
                             ymax = ci_95_upper,
                             group = covariates, color = covariates)) +
  geom_pointrange(position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = unique(results_out_h10b$mod_label),
                   labels = unique(results_out_h10b$mod_label)) +
  labs(x = "Receipt of Gifts Prior to Election", 
       y = "Treatment Effect \n(Vote Incumbent)") +
  geom_hline(yintercept=0, linetype="dashed", color = "red") +
  geom_errorbar(position = position_dodge(width = 0.5),
                mapping = aes(x = mod_label,
                              ymin = ci_90_lower,
                              ymax = ci_90_upper,
                              group = covariates, color = covariates),
                size=1.5, width=0) + 
  theme_bw(base_size=12) +
  theme(legend.position="bottom") + ylim(-0.15,0.15) +
  ggtitle("Vote Buying, Bad News")

# pdf(file="lc3-councillor-hte-cov-200629.pdf", height=8, width = 10)
ggarrange(h2a,h2b,h5a,h5b,h6a,h6b,h10a,h10b,
          ncol = 2, nrow = 4,
          labels = c("A","B","C","D","E","F","K","L"),
          common.legend = TRUE, legend = "bottom")
# dev.off()

```

## Table S6: Heterogeneous effects of treatment by LC5 chair vote margin in 2011, without covariates

```{r tabS6-hte-vote-margin-no-cov, warnings=F}

h1a.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1a.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1a.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1a.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


h1b.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1b.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1b.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1b.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


stargazer(h1a.ps.1, h1a.ps.2, h1a.ps.3, h1a.ps.4, h1b.ps.1, h1b.ps.2, h1b.ps.3, h1b.ps.4, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:lc5_chair_margin_2011"),
          covariate.labels = c("Treatment", "Treatment X Margin"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "No", "No","No","No","No", "No","No","No")),
          dep.var.caption  = "<em>DV: Vote Choice for the Incumbent; Intention as Control<em>",
          notes = "<em>Note:</em> SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) # Thu, Dec 17, 2020 - 10:26:02

```

## Table S7: Heterogeneous effects of treatment by LC5 chair vote margin in 2011, with covariates

```{r tabS7-hte-vote-margin-cov, warnings=F}

h1a.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1a.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1a.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1a.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


h1b.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1b.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1b.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1b.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*lc5_chair_margin_2011 + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


stargazer(h1a.ps.1.cov, h1a.ps.2.cov, h1a.ps.3.cov, h1a.ps.4.cov, h1b.ps.1.cov, h1b.ps.2.cov, h1b.ps.3.cov, h1b.ps.4.cov, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:lc5_chair_margin_2011"),
          covariate.labels = c("Treatment", "Treatment X Margin"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes")),
          dep.var.caption  = "<em>DV: Vote Choice for the Incumbent; Intention as Control<em>",
          notes = "<em>Note:</em> SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) # Thu, Dec 17, 2020 - 11:03:07

```

## Table S8: Heterogeneous effects of treatment by NRM incumbency, without covariates

```{r tabS8-h1-no-cov-hte-nrm, warning=F}

data <- data %>%
  mutate(lc3.chair.nrm = ifelse(lc3.chair.party=="nrm",1,0),
         lc3.councillor.nrm = ifelse(lc3.councillor.party=="nrm",1,0))

data <- data %>% mutate(nrm.temp=lc5.chair.nrm)
h1a.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*nrm.temp + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(nrm.temp=lc5.councillor.nrm)
h1a.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*nrm.temp + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(nrm.temp=lc3.chair.nrm)
h1a.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*nrm.temp + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(nrm.temp=lc3.councillor.nrm)
h1a.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*nrm.temp + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


data <- data %>% mutate(nrm.temp=lc5.chair.nrm)
h1b.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*nrm.temp + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(nrm.temp=lc5.councillor.nrm)
h1b.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*nrm.temp + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(nrm.temp=lc3.chair.nrm)
h1b.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*nrm.temp + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(nrm.temp=lc3.councillor.nrm)
h1b.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*nrm.temp + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1, h1a.ps.2, h1a.ps.3, h1a.ps.4, h1b.ps.1, h1b.ps.2, h1b.ps.3, h1b.ps.4, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:nrm.temp"),
          covariate.labels = c("Treatment", "Treatment X NRM"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "No", "No","No","No","No", "No","No","No")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Thu, Dec 17, 2020 - 12:49:15

```


## Table S9: Heterogeneous effects of treatment by NRM incumbency, with covariates

```{r tabS9-h1-cov-hte-nrm, message=FALSE, warning=FALSE}

data <- data %>% mutate(nrm.temp=lc5.chair.nrm)
h1a.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*nrm.temp + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(nrm.temp=lc5.councillor.nrm)
h1a.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*nrm.temp + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(nrm.temp=lc3.chair.nrm)
h1a.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*nrm.temp + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(nrm.temp=lc3.councillor.nrm)
h1a.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*nrm.temp + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


data <- data %>% mutate(nrm.temp=lc5.chair.nrm)
h1b.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*nrm.temp + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(nrm.temp=lc5.councillor.nrm)
h1b.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*nrm.temp + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(nrm.temp=lc3.chair.nrm)
h1b.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*nrm.temp + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(nrm.temp=lc3.councillor.nrm)
h1b.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*nrm.temp + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1.cov, h1a.ps.2.cov, h1a.ps.3.cov, h1a.ps.4.cov, h1b.ps.1.cov, h1b.ps.2.cov, h1b.ps.3.cov, h1b.ps.4.cov, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:nrm.temp"),
          covariate.labels = c("Treatment", "Treatment X NRM"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Thu, Dec 17, 2020 - 12:54:31

```

## Table S10: Heterogeneous effects of treatment by trust in Twaweza as an information source, without covariates

```{r tabS10-h1-no-cov-hte-trust_twaweza, warning=F, message=F}

data <- data %>%
  mutate(b.trust.Twaweza = factor(b.trust.Twaweza,
                                  levels = c("trust_a_lot",
                                             "trust_a_little",
                                             "don_t_know",
                                             "do_not_trust_a",
                                             "missing")),
         b.trust.Twaweza.bin = ifelse(b.trust.Twaweza == "trust_a_lot",
                                      1, 0))


h1a.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1a.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1a.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1a.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


h1b.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1b.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1b.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1b.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1, h1a.ps.2, h1a.ps.3, h1a.ps.4, h1b.ps.1, h1b.ps.2, h1b.ps.3, h1b.ps.4, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:b.trust.Twaweza.bin"),
          covariate.labels = c("Treatment", "Treatment X Trust Twaweza"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "No", "No","No","No","No", "No","No","No")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Mon, Jan 25, 2021 - 08:32:44

```

## Table S11: Heterogeneous effects of treatment by trust in Twaweza as an information source, with covariates

```{r tabS11-h1-cov-hte-trust_twaweza, message=FALSE, warning=FALSE}


h1a.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1a.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1a.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1a.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


h1b.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

h1b.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

h1b.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

h1b.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*b.trust.Twaweza.bin + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1.cov, h1a.ps.2.cov, h1a.ps.3.cov, h1a.ps.4.cov, h1b.ps.1.cov, h1b.ps.2.cov, h1b.ps.3.cov, h1b.ps.4.cov, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:b.trust.Twaweza.bin"),
          covariate.labels = c("Treatment", "Treatment X Trust Twaweza"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Mon, Jan 25, 2021 - 08:50:03

```


## Table S12: Heterogeneous effects of treatment by public service subgroup with attribution information, without covariates

```{r tabS12-attribution-hte-no-cov, message=F, warning=F}

data <- data %>%
  mutate(lc5.attribution = ifelse(finalps %in% 
                                    c("roads.0","roads.1","education.0","education.1"),
                                  1,0),
         lc3.attribution = ifelse(finalps %in% 
                                    c("roads.0","roads.1"),
                                  1,0))

## *** Without covariates 
data <- data %>% mutate(attribution.temp=lc5.attribution)
h1a.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*attribution.temp + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(attribution.temp=lc5.attribution)
h1a.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*attribution.temp + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1a.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*attribution.temp + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1a.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*attribution.temp + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))


data <- data %>% mutate(attribution.temp=lc5.attribution)
h1b.ps.1 <- felm(lc5.chair.inc.vote ~ ps.treat*attribution.temp + lc5.chair.intent | location.id | 0 | lc5.chair.name, 
                 data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(attribution.temp=lc5.attribution)
h1b.ps.2 <- felm(lc5.councillor.inc.vote ~ ps.treat*attribution.temp + lc5.councillor.intent | location.id | 0 | lc5.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1b.ps.3 <- felm(lc3.chair.inc.vote ~ ps.treat*attribution.temp + lc3.chair.intent | location.id | 0 | lc3.chair.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1b.ps.4 <- felm(lc3.councillor.inc.vote ~ ps.treat*attribution.temp + lc3.councillor.intent | location.id | 0 | lc3.councillor.name,
                 data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1, h1a.ps.2, h1a.ps.3, h1a.ps.4, h1b.ps.1, h1b.ps.2, h1b.ps.3, h1b.ps.4, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:attribution.temp"),
          covariate.labels = c("Treatment", "Treatment X Attribution"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "No", "No","No","No","No", "No","No","No")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Mon, Dec 21, 2020 - 10:43:29

```


## Table S13: Heterogeneous effects of treatment by public service subgroup with attribution information, with covariates

```{r tabS13-attribution-hte-cov, message=F, warning=F}

## *** With covariates 
data <- data %>% mutate(attribution.temp=lc5.attribution)
h1a.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*attribution.temp + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.good.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(attribution.temp=lc5.attribution)
h1a.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*attribution.temp + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1a.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*attribution.temp + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.good.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1a.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*attribution.temp + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.good.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

data <- data %>% mutate(attribution.temp=lc5.attribution)
h1b.ps.1.cov <- felm(lc5.chair.inc.vote ~ ps.treat*attribution.temp + lc5.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.chair.name, 
                     data=filter(data, ps.bad.noNA==1 & lc5.chair.competitive==1 & lc5.chair.party.switch==0))

data <- data %>% mutate(attribution.temp=lc5.attribution)
h1b.ps.2.cov <- felm(lc5.councillor.inc.vote ~ ps.treat*attribution.temp + lc5.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc5.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc5.councillor.competitive==1 & lc5.councillor.party.switch==0 & is.na(lc5.councillor.redistricted2016)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1b.ps.3.cov <- felm(lc3.chair.inc.vote ~ ps.treat*attribution.temp + lc3.chair.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.chair.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.chair.competitive==1 & lc3.chair.party.switch==0 & is.na(lc3.chair.redistricted)))

data <- data %>% mutate(attribution.temp=lc3.attribution)
h1b.ps.4.cov <- felm(lc3.councillor.inc.vote ~ ps.treat*attribution.temp + lc3.councillor.intent + b.Q1_living_conditions + b.Q21b_Trust_in_Twaweza_for_info + b.Q24_Powerful_ppl_learning_how_ + b.Q25_Will_counting_votes_be_fai + r.What_is_your_gender + r.education + r.What_is_your_age | location.id | 0 | lc3.councillor.name,
                     data=filter(data, ps.bad.noNA==1 & lc3.councillor.competitive==1 & lc3.councillor.party.switch==0 & is.na(lc3.councillor.redistricted2016)))

stargazer(h1a.ps.1.cov, h1a.ps.2.cov, h1a.ps.3.cov, h1a.ps.4.cov, h1b.ps.1.cov, h1b.ps.2.cov, h1b.ps.3.cov, h1b.ps.4.cov, type = "html", 
          dep.var.labels = c("LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor", "LC5 Chair", "LC5 Councillor", "LC3 Chair", "LC3 Councillor"),
          column.labels = c("Good News","Bad News"), column.separate = c(4,4),
          keep = c("ps.treat","ps.treat:attribution.temp"),
          covariate.labels = c("Treatment", "Treatment X Attribution"),
          add.lines = list(c("Village fixed effects", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes"),
                           c("Covariates", "Yes", "Yes","Yes","Yes","Yes", "Yes","Yes","Yes")),
          dep.var.caption  = "DV: Vote Choice for the Incumbent; Intention as Control",
          notes = "Note: SEs clustered by politician and two-tailed p-values",
          notes.label = "", notes.append = FALSE,
          df = FALSE, omit.stat = c("rsq","ser"),
          report = c('vcsp')
) #Mon, Dec 21, 2020 - 11:12:36

```

## Figure S13: Descriptive data for voters’ priors about preferred public services

```{r figS13-prior-plot, message=FALSE, warning=FALSE, message=FALSE, results='asis'}

data$f.finalps.prior=factor(data$finalps.prior.store, labels=c("Much Worse (1)","A Little Worse (2)", "Don't Know (3)", "Better (4)", "Much Better (5)"))


# pdf("./ps_prior_210405.pdf",width=6,height=5)
ggplot(data[!is.na(data$f.finalps.prior),], aes(f.finalps.prior))+
  geom_bar(position = "dodge", fill="black")+
  theme_bw()+
  theme(axis.text = element_text(size = 12),
        axis.title = element_text(size = 14, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)
  )+  labs(x = "Respondent Prior Beliefs about Preferred Public Service", y="Number of Responses")

# dev.off()
# knitr::include_graphics("./figures/ps_prior.jpg")

```

## Figure S14: Distribution of audit performance scores for respondents’ selected public service

```{r figS14-audit-plots, message=FALSE, warning=FALSE,  echo=FALSE, message=FALSE,results='asis'}

#prior and posteriors as factors with labels

data$f.finalps.actual=factor(data$finalps.actual, labels=c("Much Worse (1)","A Little Worse (2)", "Better (4)", "Much Better (5)"))

# jpeg("./Figures/ps_audit.jpg",width=500,height=500, quality=100)

ggplot(data[!is.na(data$f.finalps.actual),], aes(f.finalps.actual))+
  geom_bar(position = "dodge", fill="black")+
  theme_bw()+
  theme(axis.text = element_text(size = 12),
        axis.title = element_text(size = 14, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)
  )+  labs(x = "Public Services Audit Performance", y="Number of Responses")

# dev.off()
# knitr::include_graphics("./figures/ps_audit.jpg")

```

## Figure S15: Difference between audits and respondents’ prior beliefs about public service quality

```{r figS15-prior-audit-plots, results='asis', fig.width=10, fig.height=5}

### Difference between audit and voters prior budget performance scores

# data$finalps.post=data$sc.quality_PS_compared
# data$finalps.post_int=NA
# data$finalps.post_int=ifelse(data$finalps.post=="don_t_know", 3, data$finalps.post_int)
# data$finalps.post_int=ifelse(data$finalps.post=="much_worse", 1, data$finalps.post_int)
# data$finalps.post_int=ifelse(data$finalps.post=="a_little_worse", 2, data$finalps.post_int)
# data$finalps.post_int=ifelse(data$finalps.post=="better", 4, data$finalps.post_int)
# data$finalps.post_int=ifelse(data$finalps.post=="much_better", 5, data$finalps.post_int)
# data$finalps.post=data$finalps.post_int

data$ps.diff.prior=data$finalps.actual-as.numeric(data$finalps.prior.store)
#note: changed this to stored variable of non-imputed priors
#note: this is the exact same variable as `finalps.prior.diff.store`

# data$ps.diff.post=data$finalps.actual-data$finalps.post

data$worseaudit=ifelse(data$finalps.actual>=3, 0, 1)

data.temp=data[, c("ps.diff.prior", "worseaudit", "ps.treat")]

data.temp$countme=1
data.temp=aggregate(data.temp, by=list(ps.diff.prior.x=data.temp$ps.diff.prior, worse.x=data.temp$worseaudit, ps.treat.x=data.temp$ps.treat), sum)
data.temp$worse.x=factor(data.temp$worse.x, labels=c("Worse than Average \nAudit (Audit Score<3)", "Better than Average \nAudit (Audit Score>3)"))

cbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

treat.plot=ggplot(data.temp[data.temp$ps.treat.x==1,], aes(x=ps.diff.prior.x, y=countme, fill=worse.x))+
  geom_col()+  
  theme_bw()+
  theme(axis.text = element_text(size = 12),
        axis.title = element_text(size = 12, face="bold"),
        plot.title = element_text(size = 12, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
  )+  labs(x = "Audit Score - Prior Belief", y="Number of Responses")+
  scale_x_continuous(limits=c(-4.5, 4.5), breaks=c(-4,-3,-2,-1,0, 1, 2, 3,4))+
  ggtitle(paste("Treatment Group (N=", sum(data.temp[data.temp$ps.treat.x==1,]$countme), ")", sep=""))+
  guides(fill=FALSE)+
  scale_fill_manual(values=cbPalette)


control.plot=ggplot(data.temp[data.temp$ps.treat.x==0,], aes(x=ps.diff.prior.x, y=countme, fill=worse.x))+
  geom_col()+  
  theme_bw()+
  theme(axis.text = element_text(size = 12),
        axis.title = element_text(size = 12, face="bold"),
        plot.title = element_text(size = 12, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 13)
  )+  labs(x = "Audit Score - Prior Belief", y="Number of Responses")+
  scale_x_continuous(limits=c(-4.5, 4.5), breaks=c(-4,-3,-2,-1,0, 1, 2, 3,4))+
  ggtitle(paste("Control Group (N=", sum(data.temp[data.temp$ps.treat.x==0,]$countme), ")", sep=""))+
  guides(fill=guide_legend(title="", keywidth=0.3,
                           keyheight=0.6,
                           default.unit="inch"))+
  scale_fill_manual(values=cbPalette)

# jpeg("./ps_auditminusprior_210405.jpg",width=1000,height=500, quality=300)
# grid.arrange(treat.plot, control.plot, ncol=2, widths=c(555,1010))
# dev.off()
# note: this is the output code, below is the code for Rmarkdown

grid.arrange(treat.plot, control.plot, ncol=2, widths=c(4,8))

# knitr::include_graphics("./figures/ps_auditminusprior.jpg")

data.temp=NULL

```

## Figure S16: Balance of individual-level pre-treatment covariates at baseline

``` {r figS16-balance-baseline, fig.height=11, fig.width=8.5}

balance.plot <- function(dta, var, treat, title){
  out <-  ggplot(data=dta, aes_string(x = var, fill = treat)) + 
    geom_bar(data=dta[dta[,treat]==1,], fill="goldenrod2") + 
    geom_bar(data=dta[dta[,treat]==0,] ,aes(y=..count..*(-1)), fill="#000000") + 
    scale_y_continuous(breaks=seq(-2000,2000,2000),labels=abs(seq(-2000,2000,2000))) +
    coord_flip() + theme(legend.position="none") +
    ggtitle(title) +
    theme(plot.title = element_text(hjust = 0.5, size=9), axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.y = element_text(size=5),
          axis.text.x = element_text(size=9)
          )
  return(out)
}

#adding subcounty attrition:
data$sc.effort_LC3councilor.y <- ifelse(data$sc.effort_LC3councilor.y==0,
                                        NA,
                                        data$sc.effort_LC3councilor.y)

data$sc.attr <- NA
for (i in 1:nrow(data)){
  data$sc.attr[i] <- ifelse(sum(!is.na(data[i,78:121]))>0,0,1)
}


###Cleaning the data and levels
covs0 <- data[,c("ps.treat",
                 "b.Q1_living_conditions",
                 "lc5.councillor.same.tribe",
                 "lc5.turnout.intent",
                 "lc5.councillor.intent",
                 "lc5.chair.intent",
                 "lc3.turnout.intent",
                 "lc3.councillor.intent",
                 "lc3.chair.intent",
                 "partyID",
                 "party.attachment",
                 "b.Q11_2011_Vote_for_LC5councilor",
                 "b.Q12_2011_vote_for_LC5chair",
                 "b.Q7_vote_for_current_LC3council",
                 "b.Q8_vote_for_current_LC3chair",
                 "b.vote.buying",
                 "budget.importance",
                 "ps.importance",
                 "b.Q18_Most_important_public_serv",
                 "b.Q19_Second_most_important_publ",
                 "b.Q21a_Trust_in_politicians_for_",
                 "b.trust.Twaweza",
                 "b.Q21c_Trust_in_NGOs_for_info",
                 "b.trust.AG",
                 "b.Q22_LC5_record_of_budget_mgmt",
                 "budget.certainty",
                 "b.Q24_Powerful_ppl_learning_how_",
                 "b.Q25_Will_counting_votes_be_fai",
                 "r.What_is_your_gender",
                 "r.education",
                 "r.In_which_language_would_you_pr",
                 "r.What_is_your_age",
                 "finalps.prior",
                 "d.attr",
                 "sc.attr"
)
] 

var.labs = c("PS treatment", "Living Conditions", "Same Tribe LC5 Councillor", 
"LC5 Turnout Intent", "LC5 Councillor Intent", "LC5 Chair Intent", 
"LC3 Turnout Intent", "LC3 Councillor Intent", "LC3 Chair Intent", 
"Party ID", "Party Attachment", "Voted for LC5 Councillor in 2011", 
"Voted for LC5 Chair in 2011", "Voted for LC3 Councillor in 2011", 
"Voted for LC3 Chair in 2011", "Vote Buying", "Importance of Budget", 
"Importance of Public Sercice", "Most Important Public Service", 
"Second Most Important Public Service", "Trust in Local Politicians", 
"Trust in Twaweza", "Trust in NGOs", "Trust in Auditor General", 
"Budget Prior", "Budget Certainty", "Voting Secret", "Voting Fair", 
"Gender", "Education", "Preferred Language", "Age", "Public Services Prior", 
"District Election Attrition","Subcounty Election Attrition")

##Changing variable names for plot labeling
#names(covs0)
var.names=data.frame(var.name=names(covs0), var.label=var.labs)
#write.csv(var.names, "labels.csv")
# var.names=read.csv("labels.csv", stringsAsFactors = FALSE)

covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"]%in% c(1:24), "<25", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(25:35), "25-35", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(36:45), "36-45", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(46:65), "46-65", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(66:75), "66-75", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(76:85), "76-85", covs0[,"r.What_is_your_age"])
covs0[,"r.What_is_your_age"]=ifelse(covs0[,"r.What_is_your_age"] %in% c(86:100), "86-100", covs0[,"r.What_is_your_age"])

covs0$finalps.prior <- round(as.numeric(covs0$finalps.prior))
covs0 <- covs0 %>%
  mutate(finalps.prior = case_when(finalps.prior == 1 ~ "much worse",
                                   finalps.prior == 2 ~ "a little worse",
                                   finalps.prior == 3 ~ "don't know",
                                   finalps.prior == 4 ~ "better",
                                   finalps.prior == 5 ~ "much better"))

cols <- 2:(ncol(covs0)-3)
covs0[,cols] = apply(covs0[,cols], 2, function(x) (as.character(x)))

covs0[covs0=="don_t_know"] <- "don't know"
covs0[covs0=="much_better"] <- "much better"
covs0[covs0=="much_worse"] <- "much worse"
covs0[covs0=="refused_to_ans"] <- "refused"
covs0[covs0=="the_same"] <- "same"
covs0[covs0=="completed_mast"] <- "complete graduate"
covs0[covs0=="completed_prim"] <- "complete primary"
covs0[covs0=="completed_seco"] <- "complete secondary"
covs0[covs0=="completed_univ"] <- "complete university"
covs0[covs0=="no_schooling"] <- "no schooling"
covs0[covs0=="refuse_to_answ"] <- "refused"
covs0[covs0=="some_post_grad"] <- "some graduate"
covs0[covs0=="some_primary_s"] <- "some primary"
covs0[covs0=="some_secondary"] <- "some secondary"
covs0[covs0=="some_universit"] <- "some university"
covs0[covs0=="not_at_all_lik"] <- "not at all likely"
covs0[covs0=="not_very_likel"] <- "not very likely"
covs0[covs0=="very_likely"] <- "very likely"
covs0[covs0=="a_little_worse"] <- "a little worse"
covs0[covs0=="not_certain"] <- "not certain"
covs0[covs0=="very_certain"] <- "very certain"
covs0[covs0=="very_uncertain"] <- "very uncertain"
covs0[covs0=="i_did_not_vote"] <- "didn't vote"
covs0[covs0=="somewhat_likel"] <- "somewhat likely"
covs0[covs0=="somewhat_unlik"] <- "somewhat unlikely"
covs0[covs0=="very_likel"] <- "very likely"
covs0[covs0=="very_unlikely"] <- "very unlikely"
covs0[covs0=="not_important"] <- "not important"
covs0[covs0=="not_very_impor"] <- "not very important"
covs0[covs0=="somewhat_impor"] <- "somewhat important"
covs0[covs0=="very_important"] <- "very important"
covs0[covs0=="local_health_services"] <- "health"
covs0[covs0=="local_roads"] <- "roads"
covs0[covs0=="primary_schools"] <- "schools"
covs0[covs0=="water_access"] <- "water"
covs0[covs0=="refused_to_answer"] <- "refused"
covs0[covs0=="local_health_s"] <- "health"
covs0[covs0=="primary_school"] <- "schools"
covs0[covs0=="do_not_trust_a"] <- "don't trust at all"
covs0[covs0=="option_5"] <- "refused"
covs0[covs0=="trust_a_little"] <- "trust a little"
covs0[covs0=="trust_a_lot"] <- "trust a lot"
covs0[covs0=="other-noincumbentpartymatch"] <- "other"

covs0[,"r.In_which_language_would_you_pr"] <- ifelse(covs0[,"r.In_which_language_would_you_pr"]!="luganda" & covs0[,"r.In_which_language_would_you_pr"]!="english" & covs0[,"r.In_which_language_would_you_pr"]!="runyankole" & covs0[,"r.In_which_language_would_you_pr"]!="langi" & covs0[,"r.In_which_language_would_you_pr"]!="ateso", "other", covs0[,"r.In_which_language_would_you_pr"])
covs0[covs0=="AUTOMATIC"] <- "other"


covs0=data.frame(apply(covs0, 2, function(x) x=ifelse(is.na(x) | x=="" | x==" ", "missing", x)))

for(this.var in c("lc5.councillor.same.tribe", "lc5.turnout.intent", "lc5.councillor.intent", "lc3.councillor.intent", "lc3.chair.intent")){
  covs0[,this.var]=factor(covs0[,this.var], labels = c("no", "yes", "missing"))
}


###Balance at baseline (before attrition)
df <- data.frame(x=c(4,4),y=c(6,4),treat=c(1,0))
text.df <- data.frame(x=c(5,5),y=c(6,4),label=c("Treatment","Control"))
leg <- ggplot(df) + geom_point(shape=15, size=5, x=df$x, y=df$y, aes(colour=factor(df$treat))) + xlim(0, 10) + ylim(0, 10) + 
  scale_colour_manual(values=c("black", "goldenrod2"))+
  geom_text(aes(x=text.df$x, y=text.df$y, label=text.df$label), hjust=0, vjust=0.4, size=5) +
  theme(axis.line=element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.background=element_blank())

# pdf("../figures/ps_balance.pdf",height=11, width=8.5)
plot.list <- list()

count=0
for (i in 1:(nrow(var.names)-2)){
  count=count+1
  plot.list[[count]] <- balance.plot(dta=covs0, var=var.names[i,]$var.name, treat="ps.treat", title=var.names[i,]$var.label)
}
plot.list[[i+1]]=leg
plot.list[[1]]=NULL
grid.arrange(grobs=plot.list, ncol=4)


# dev.off()

# knitr::include_graphics("./figures/ps_balance.pdf")

### Balance F-Test ----

rhs <- paste(names(covs0[2:33]), collapse=" + ")
fml <- paste("as.numeric(ps.treat) ~ ",rhs,sep = "")

balance_mod <- lm(formula = fml, data = covs0)
# xtable(balance_mod) #much evidence of imbalance

#table(covs0$ps.treat,covs0$finalps.prior) #have to source step down before proceeding

```


## Table S14: Attrition with respect to the public services treatment

```{r tabS14-tab-attrition}

#note: the table was created manually in LaTex based on the values below

data$sc.effort_LC3councilor.y <- ifelse(data$sc.effort_LC3councilor.y==0,
                                        NA,
                                        data$sc.effort_LC3councilor.y)

data$sc.attr <- NA
for (i in 1:nrow(data)){
  data$sc.attr[i] <- ifelse(sum(!is.na(data[i,78:121]))>0,0,1)
}

#district attrition table:
d.attr.tab <- table(data$ps.treat,data$d.attr)
kable(d.attr.tab)
chisq.test(d.attr.tab) #No indication of differential attrition

d.attr.prop <- prop.table(d.attr.tab, margin=1)
kable(d.attr.prop)

#table(data$d.attr,data$d.X_11_Over_the_last_several_days, useNA = "always")
#note: why are there 838 cases with no attrition and no recorded message
#see <- subset(data, d.attr==0 & is.na(d.X_11_Over_the_last_several_days))
#The following instructions attached to Q11 likely confused enumerators (mistakenly attached to Q11, instead of Q12):
# This last question is only for subjects that are assigned a reinforcement message. On the contact sheet there is a column including the public service that is most important to the subject, and the score for the public service. If these columns are empty for a subject, skip question 12. If these columns have values, plug in these values to the messages below.


#subcounty attrition table:
sc.attr.tab <- table(data$ps.treat,data$sc.attr)
kable(sc.attr.tab)
chisq.test(sc.attr.tab) #No indication of differential attrition

sc.attr.prop <- prop.table(sc.attr.tab, margin=1)
kable(sc.attr.prop)

```
